這篇文章主要介紹了AngularJS實現的簡單拖曳功能,涉及AngularJS事件響應及頁面元素屬性動態操作相關實現技巧,需要的朋友可以參考下
本文實例講述了AngularJS實現的簡單拖曳功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>www.jb51.net AngularJS拖拽</title> <style> *{ padding:0; margin:0; } .wei{ width:100px; height:100px; background: red; position:absolute; cursor: pointer; /*left:0;top:0;*/ } </style> </head> <body ng-controller="show"> <p class="wei" wei-yi data="true"></p> <p class="wei" wei-yi data="false"></p> <script src="jquery.js" type="text/javascript" charset="utf-8"></script> <script src="angular.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var app = angular.module('myApp',[]); //自定义属性 app.directive("weiYi",function(){ return{ restrict :'A',//A属性,E标签,C类名,D注释 link :function(scope,element,attr){ attr.data=angular.equals(attr.data,"true"); //console.log(attr.data); console.log(element); element.on("mousedown",function(e){ var that = $(this); console.log(attr.data); if(attr.data){ $p=$("<p>"); console.log($p); $p.css({"width":"100px","height":"100px","border": "2px dotted green","position":"absolute","left":that.offset().left,"top":that.offset().top}); $p.appendTo($("body")); } var x=e.clientX-$(this).offset().left; var y=e.clientY-$(this).offset().top; //console.log(x+":"+y); $(document).on("mousemove",function(e){ if(attr.data){ $p.css({"left":e.clientX-x,"top":e.clientY-y}); }else{ that.css({"left":e.clientX-x,"top":e.clientY-y}); } }); $(document).on("mouseup",function(e){ //console.log($p); $(document).off(); if(attr.data){ that.css({"left":$p.offset().left,"top":$p.offset().top}); $p.remove(); } }) }) } } }); app.controller('show',['$scope',function(scope$){ }]); </script> </body> </html>
運行效果如下:
上面是我整理給大家的,希望今後會對大家有幫助。
相關文章:
以上是在AngularJS中如何實現拖曳功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!