Custom Directive
How do I make an custom directive?
What is a custom directive? How do you make a custom directive? This script and codes were developed by Robert on 09 January 2023, Monday.
Custom Directive - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>custom Directive</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <body ng-app="ngApp" ng-controller="mainCtrl"> <div class="container"> <div style="display: table-row"> <div box-size="{{percentage}}"> </div> </div> <!-- <div box-size="{{percentage}}"></div> <div box-size="{{percentage}}"></div> <div box-size="{{percentage}}"></div> <div box-size="{{percentage}}"></div> <div box-size="{{percentage}}"></div> --> </div>
</body> <script src='http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Custom Directive - Script Codes CSS Codes
body { padding-right: 0px;
}
* { box-sizing: border-box;
}
.container { overflow: auto; padding: 0; margin-top: -10px; margin-left: -10px; width: 100%; display: table;
}
.box { border-left: 1px dashed lightgray; border-top: 1px dashed lightgray; padding: 5px; display: table-cell; float: left;
}
.col4 { width: 25%;
}
.col3 { width: 33%;
}
.col2 { width: 50%;
}
.col1 { width: 100%;
}
Custom Directive - Script Codes JS Codes
var app = angular.module("ngApp", []) .controller('mainCtrl', function($scope, $window) { $scope.percentage = 33; $scope.boxHeight = { 'min-height': '0px' }; $scope.storeHeight = []; $scope.friends = [{ name: "Pablo", desc: "Met in Columbia, while working for the Navy." }, { name: "Carlos", desc: "Met in Yale University, while doing my doctorate. Very knowledgeable guy. Great Friend." }, { name: "Peter", desc: "Was my Boss at Chase Bank. He was firm but always did it with a smile." }, { name: "Mark", desc: "One of my best friends. Good natured and was always cracking a joke." }, { name: "Pablo", desc: "Met in Columbia, while working for the Navy." }, { name: "Carlos", desc: "Met in Yale University, while doing my doctorate. Very knowledgeable guy. Great Friend." }, { name: "Peter", desc: "Was my Boss at Chase Bank. He was firm but always did it with a smile." }, { name: "Mark", desc: "One of my best friends. Good natured and was always cracking a joke." }, { name: "James", desc: "Is my nephew from my sister's side. Great kid always wants to spend time with me reading comics or just playing video games." }, { name: "Julia", desc: "One of my best friends. Good natured and was always cracking a joke." }]; $window.onresize = function() { var innerWidth = window.document.body.clientWidth; var boxes = []; var currPercent = $scope.percentage, newPercent = currPercent; if (innerWidth <= 480) { newPercent = 100; } else if (innerWidth <= 768) { newPercent = 50; } else if (innerWidth <= 1024) { newPercent = 33; } else if (innerWidth > 1024) { newPercent = 25; } if(newPercent !== currPercent ){ boxes = angular.element(document.getElementsByClassName('box')); $scope.percentage = newPercent; //console.log(boxes); for(i= 0; i < boxes.length; i++){ console.log(boxes[i].css("height")); //console.log(boxes.eq(i).children().css('height')); } // console.log(boxes, innerWidth); } console.log($scope.percentage); $scope.$apply(); } })
.directive('boxSize', function($timeout) { return { restrict: 'A', template: '<div ng-class="{\'box\' : true, \'col4\' : (percentage === 25 ), \'col3\' : (percentage === 33 ), \'col2\' : (percentage === 50 ), \'col1\' : (percentage === 100 )}"'+ 'ng-repeat="friend in friends track by $index" index="{{$index}}" id="div{{$index}}" ng-style="boxHeight"><h3>{{friend.name}}</h3><p>{{friend.desc}}</p></div>', link: function(scope, elem, attrs) { $timeout(init, false); //Initialization function init() { console.log('offsetHeight = ' + elem[0].offsetHeight + ' ' + elem[0].index); } } } function getWidth() { return { width: attrs.boxSize + "%" }; } function _test(percent) { console.log(percent); }
})
.directive('rrHeightAdjuster', function($timeout) { return { link: function(scope, elem, attr) { //console.log(elem[0]); scope.boxHeight['min-height'] = '0px'; $timeout(function() { scope.storeHeight.push(elem[0].clientHeight); //console.log(angular.element(document.querySelector(attr.id +' h3')).innerHeight); console.log(elem.find(attr.id)); var currHeight = parseInt(scope.boxHeight['min-height']), elemHeight = elem[0].clientHeight; if (currHeight < elemHeight) { currHeight = elemHeight; console.log("height: " + currHeight); } //elem[0].css({'height' : (currHeight + 10) +"px"}); scope.boxHeight['min-height'] = currHeight + 'px'; console.log("HEIGHTS: " + scope.storeHeight ); }, 0); } }
})
Developer | Robert |
Username | AgentRR007 |
Uploaded | January 09, 2023 |
Rating | 3 |
Size | 3,425 Kb |
Views | 6,072 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
UI Challenge - Menu Expander | 2,387 Kb |
CountDown | 4,688 Kb |
DC Comic Slide Image | 3,262 Kb |
Angular Template | 2,104 Kb |
Interface Animation | 3,829 Kb |
AgentRR007 Logo Rotate | 1,874 Kb |
RegExp in Angular Tester | 3,618 Kb |
Challenge Reveal... with a twist. | 2,306 Kb |
A Little Snake Animation | 4,738 Kb |
FireBase Demo | 3,081 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Personal Website Redesign v2.0 | DevItWithDavid | 5,168 Kb |
Birthday Party Starter | Aussieyang | 1,629 Kb |
Css3 loader | Clknap | 2,391 Kb |
404 Page | Saransh | 2,666 Kb |
Testimonial Fancy tabs responsive | Amit-webdesigner | 3,056 Kb |
Rotate Demo | Agelber | 3,061 Kb |
Fluid Grid 12 | Alexoliverwd | 2,309 Kb |
Slick Slider | Wearebold | 5,913 Kb |
Automatic scroll | Skeurentjes | 4,042 Kb |
Scifi-style Interative Form | Aaronchuo | 4,566 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!