Draggables in pure angular
How do I make an draggables in pure angular?
Bounded click and drag solution implemented using angularjs.. What is a draggables in pure angular? How do you make a draggables in pure angular? This script and codes were developed by Robert Lowe on 23 July 2022, Saturday.
Draggables in pure angular - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Draggables in pure angular</title> <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css'>
<link rel='stylesheet prefetch' href='https://cdn.jsdelivr.net/hint.css/1.3.5/hint.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="app-container" ng-app="draggableContainer" ng-controller="draggableContainerCtrl" ng-mouseup="onGlobalMouseUp($event)" ng-mousemove="onGlobalMouseMove($event)"> <!-- numeric values are in em--> <!-- d--> <draggable-box startx="1" starty="3" classname="rectangle-large" color="#477998"></draggable-box> <draggable-box startx="3" starty="1" classname="rectangle-large" color="#477998"></draggable-box> <draggable-box startx="1" starty="3" classname="rectangle-large horizontal" color="#78DB69"></draggable-box> <draggable-box startx="1" starty="5" classname="rectangle-large horizontal" color="#FFC145"></draggable-box> <draggable-box startx="3" starty="3" classname="rectangle-large" color="#F64740"></draggable-box> <draggable-box startx="3" starty="1" classname="square" color="#78DB69"></draggable-box> <!-- r--> <draggable-box startx="4.5" starty="3" classname="rectangle-large" color="#78DB69"></draggable-box> <draggable-box startx="4.5" starty="3" classname="rectangle horizontal" color="#477998"></draggable-box> <!-- a--> <draggable-box startx="7.5" starty="5" classname="rectangle-large horizontal" color="#78DB69"></draggable-box> <draggable-box startx="7" starty="3" classname="rectangle-large" color="#FFC145"></draggable-box> <draggable-box startx="7" starty="3" classname="rectangle horizontal" color="#F64740"></draggable-box> <draggable-box startx="9" starty="3" classname="rectangle-large" color="#477998"></draggable-box> <draggable-box startx="7" starty="5" classname="square" color="#477998"></draggable-box> <!-- g--> <draggable-box startx="11" starty="3" classname="rectangle-large" color="#F64740"></draggable-box> <draggable-box startx="11" starty="3" classname="rectangle-large horizontal" color="#477998"></draggable-box> <draggable-box startx="13" starty="3" classname="rectangle-large" color="#FFC145"></draggable-box> <draggable-box startx="11" starty="5" classname="rectangle-large horizontal" color="#477998"></draggable-box> <draggable-box startx="13" starty="5" classname="rectangle-large" color="#F64740"></draggable-box> <draggable-box startx="11" starty="7" classname="rectangle horizontal" color="#477998"></draggable-box> <draggable-box startx="13" starty="7" classname="square" color="#FFC145"></draggable-box> <draggable-box startx="11" starty="7" classname="square" color="#F64740"></draggable-box> <!-- m--> <draggable-box startx="2.5" starty="8" classname="rectangle-large" color="#F64740"></draggable-box> <draggable-box startx="5.5" starty="8" classname="rectangle-large" color="#477998"></draggable-box> <draggable-box startx="4" starty="7.5" classname="rectangle" color="#FFC145"></draggable-box> <draggable-box startx="2.5" starty="7.25" classname="rectangle-large horizontal" color="#477998"></draggable-box> <draggable-box startx="4.5" starty="7.25" classname="rectangle horizontal" color="#78DB69"></draggable-box> <!-- e--> <draggable-box startx="7" starty="7.25" classname="rectangle-large horizontal" color="#F64740"></draggable-box> <draggable-box startx="7" starty="7.75" classname="rectangle-large" color="#477998"></draggable-box> <draggable-box startx="9" starty="7.5" classname="rectangle" color="#78DB69"></draggable-box> <draggable-box startx="7" starty="8.5" classname="rectangle horizontal" color="#FFC145"></draggable-box> <draggable-box startx="7" starty="10" classname="rectangle-large horizontal" color="#F64740"></draggable-box>
</div> <script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.0/angular-sanitize.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Draggables in pure angular - Script Codes CSS Codes
*, *::before, *::after { box-sizing: border-box;
}
body { font-family: 'Open Sans', sans-serif; margin: 0; padding: 0; background-color: #495E45; font-size: 66.5%;
}
.app-container { position: relative; background-color: #607b5b; height: 95vh; border: 0.2em solid #fff; margin: 2em; background: url("http://subtlepatterns2015.subtlepatterns.netdna-cdn.com/patterns/assault.png");
}
.draggable-box { position: absolute; font-size: 3em; width: 1em; height: 1em; background-color: white; cursor: pointer; opacity: 0.8; border: 0.05em solid white;
}
.draggable-box.square { width: 1em; height: 1em;
}
.draggable-box.rectangle { width: 1em; height: 2em;
}
.draggable-box.rectangle.horizontal { width: 2em; height: 1em;
}
.draggable-box.rectangle-large { width: 1em; height: 3em;
}
.draggable-box.rectangle-large.horizontal { width: 3em; height: 1em;
}
.draggable-box.circle { width: 1.25em; height: 1.25em; border-radius: 50%;
}
.draggable-box.mousedown { box-shadow: 0 0 0.3em rgba(0, 0, 0, 0.4); opacity: 1; cursor: move;
}
Draggables in pure angular - Script Codes JS Codes
var DragModule = angular.module('dragModule', ['ngSanitize']);
DragModule.factory('dragService', function() { var s = {}; /*********************** Setup and Render ***********************/ s.setup = function($scope, element, attrs) { $scope.element = element[0]; $scope.elementParent = angular.element($scope.element).parent()[0]; s.setupPosition($scope, attrs); s.setupSize($scope); s.setupColor($scope, attrs); $scope.$on('global-mouse-up', function(eventName, $event) { s.onMouseUp($scope, $event); }); $scope.$on('global-mouse-move', function(eventName, $event) { if (!$scope.state.selected) return; s.updatePosition($scope, $event); }); }; s.setupPosition = function($scope, attrs) { angular.extend($scope.position, $scope.defaults.position); if (attrs.startx) $scope.position.startx = parseFloat(attrs.startx); if (attrs.starty) $scope.position.starty = parseFloat(attrs.starty); $scope.position.x = $scope.position.startx; $scope.position.y = $scope.position.starty; }; s.setupSize = function($scope) { $scope.size.width = $scope.element.offsetWidth; $scope.size.height = $scope.element.offsetHeight; $scope.size.containerWidth = $scope.elementParent.offsetWidth; $scope.size.containerHeight = $scope.elementParent.offsetHeight; s.setupMarginOffsets($scope, $scope.elementParent); }; s.setupMarginOffsets = function($scope, $parent) { var style = $parent.currentStyle || window.getComputedStyle($parent); $scope.offsets.top = Math.ceil(parseFloat(style.marginTop)); $scope.offsets.left = Math.ceil(parseFloat(style.marginLeft)); $scope.offsets.bottom = Math.ceil(parseFloat(style.marginBottom)); $scope.offsets.right = Math.ceil(parseFloat(style.marginRight)); }; s.setupColor = function($scope, attrs) { if (attrs.color) $scope.color.box = attrs.color; }; /*********************** Event Handling ***********************/ s.onMouseDown = function($scope, $event) { $scope.state.mousedown = true; $scope.state.selected = true; $scope.$emit('draggable-box-selected', $scope.state); s.updatePosition($scope, $event); }; s.onMouseUp = function($scope, $event) { $scope.state.mousedown = false; $scope.state.selected = false; $scope.$emit('draggable-box-selected', null); }; s.onMouseLeave = function($scope, $event) { if (!$scope.state.selected) { $scope.state.hover = false; } }; /*********************** Position Updating ***********************/ s.updatePosition = function($scope, $event) { s.updateTopPosition($scope, $event); s.updateLeftPosition($scope, $event); }; s.updateTopPosition = function($scope, $event) { var newY = $event.pageY - ($scope.size.height / 2) - $scope.offsets.top; newY = s.getBoundedY($scope, $event, newY); angular.element($scope.element).css('top', [newY, 'px'].join('')); }; s.updateLeftPosition = function($scope, $event) { var newX = $event.pageX - ($scope.size.width / 2) - $scope.offsets.left; newX = s.getBoundedX($scope, $event, newX); angular.element($scope.element).css('left', [newX, 'px'].join('')); }; s.getBoundedX = function($scope, $event, newX) { if (newX < 0) return 0; var newXRight = $event.pageX + ($scope.size.width / 2) - $scope.offsets.left + 6; if (newXRight > $scope.elementParent.offsetWidth) return ($scope.elementParent.offsetWidth - $scope.size.width) - 4; return newX; }; s.getBoundedY = function($scope, $event, newY) { if (newY < 0) return 0; var newYBottom = $event.pageY + ($scope.size.height / 2) - $scope.offsets.top + 6; if (newYBottom > $scope.elementParent.offsetHeight) return ($scope.elementParent.offsetHeight - $scope.size.height) - 4; return newY; }; return s;
});
DragModule.directive('draggableBox', function($timeout) { return { restrict: 'E', replace: true, scope: { startx: '@', starty: '@', classname: '@' }, controller: function($scope, dragService) { $scope.service = dragService; $scope.defaults = { position: { startx: 10, starty: 10, x: 10, y: 10 } }; $scope.offsets = { top: 0, left: 0, bottom: 0, right: 0 }; $scope.state = { hover: false, mousedown: false, selected: false }; $scope.size = {}; $scope.position = {}; $scope.color = { box: '#fff' }; }, template: [ '<div class="draggable-box {{classname}}" style="top: {{position.y}}em; left: {{position.x}}em; background-color: {{color.box}}"', 'ng-class="{ hover: state.hover, mousedown: state.mousedown }"', 'ng-mouseover="state.hover = true" ', 'ng-mouseleave="service.onMouseLeave(this, $event)" ', 'ng-mousedown="service.onMouseDown(this, $event)" ', '>', '</div>' ].join(''), link: function(scope, element, attrs) { $timeout(function() { scope.service.setup(scope, element, attrs); }); } };
});
var PageApp = angular.module('draggableContainer', ['ngSanitize', 'dragModule']);
PageApp.controller('draggableContainerCtrl', function($scope) { $scope.state = { selectedBox: null }; $scope.$on('draggable-box-selected', function(eventName, box) { $scope.state.selectedBox = box; }); $scope.onGlobalMouseUp = function($event) { $scope.$broadcast('global-mouse-up', $event); $scope.state.selectedBox = null; }; $scope.onGlobalMouseMove = function($event) { if ($scope.state.selectedBox === null) return; $scope.$broadcast('global-mouse-move', $event); };
});

Developer | Robert Lowe |
Username | rlo206 |
Uploaded | July 23, 2022 |
Rating | 4 |
Size | 5,167 Kb |
Views | 36,414 |
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 |
A Pen by Robert Lowe | 4,312 Kb |
The Rainball | 4,656 Kb |
Simple changelog display with AngularJS and Flexbox | 7,502 Kb |
Dynamic UI with AngularJS attributes | 5,934 Kb |
Exploring ngForm | 7,554 Kb |
Learning to style range inputs | 4,175 Kb |
Whirlpool Spinner with 4 sizes | 3,473 Kb |
Testing Semantic UI Grid | 2,832 Kb |
Google Mic Icon | 3,388 Kb |
Interactive data table with Angular and SCSS | 9,618 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 |
CSS- UI Element States Pseudo-Classes | Tesla809 | 2,206 Kb |
Playing with transition timing | Mattgrosswork | 1,993 Kb |
CSS3 Snow Animation | NickyCDK | 1,695 Kb |
SVG Scalable Text | Said_FD | 1,451 Kb |
Loading animation | Hafizfattah | 0 Kb |
SCSS Social Icons Flat | Mattsince87 | 3,482 Kb |
TweetBox with React JS | J0zelito | 3,325 Kb |
Commuter Line Tokyu 8500 | Pedox | 7,031 Kb |
Week7 replicate | Hwcasis | 1,620 Kb |
Old calculator | Gnarfugh | 2,815 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!