Draggable directive

2,277 Kb

How do I make an draggable directive?

Draggable directive from the AngulaJS directive guide. What is a draggable directive? How do you make a draggable directive? This script and codes were developed by Yahya Kacem on 14 August 2022, Sunday.

Draggable directive Previews

Draggable directive - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Draggable directive</title> <link rel="stylesheet" href="css/style.css">
<body> <div ng-app="dragModule"> <span my-draggable>Drag ME</span>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.5/angular.min.js'></script> <script src="js/index.js"></script>

Draggable directive - Script Codes CSS Codes

.draggable { cursor: pointer; position: relative; background: lightgrey; border: 1px solid red;

Draggable directive - Script Codes JS Codes

var app = angular.module('dragModule', []), directiveConstructor = function($document) { return function(scope, element) { var x = 0, y = 0, startX = 0, startY = 0, mousemove = function (event) { y = event.pageY - startY; x = event.pageX - startX; element.css({ top: y + 'px', left: x + 'px' }); }, mouseup = function () { $document.off('mousemove', mousemove); $document.off('mouseup', mouseup); }; element.addClass('draggable'); element.on('mousedown', function(event) { // Prevent default dragging of selected content event.preventDefault(); startX = event.pageX - x; startY = event.pageY - y; $document.on('mousemove', mousemove); $document.on('mouseup', mouseup); }); }; };
app.directive('myDraggable', ['$document', directiveConstructor]);
Draggable directive - Script Codes
Draggable directive - Script Codes
Home Page Home
Developer Yahya Kacem
Username yahyaKacem
Uploaded August 14, 2022
Rating 3
Size 2,277 Kb
Views 30,360
Do you need developer help for Draggable directive?

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!

Yahya Kacem (yahyaKacem) Script Codes
Create amazing captions with AI!

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!