AngularJS實(shí)現(xiàn)的簡(jiǎn)單拖拽功能示例
本文實(shí)例講述了AngularJS實(shí)現(xiàn)的簡(jiǎn)單拖拽功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>www.dbjr.com.cn 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"> <div class="wei" wei-yi data="true"></div> <div class="wei" wei-yi data="false"></div> <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標(biāo)簽,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){ $div=$("<div>"); console.log($div); $div.css({"width":"100px","height":"100px","border": "2px dotted green","position":"absolute","left":that.offset().left,"top":that.offset().top}); $div.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){ $div.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($div); $(document).off(); if(attr.data){ that.css({"left":$div.offset().left,"top":$div.offset().top}); $div.remove(); } }) }) } } }); app.controller('show',['$scope',function(scope$){ }]); </script> </body> </html>
運(yùn)行效果如下:
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
AngularJS折疊菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了AngularJS折疊菜單實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)折疊菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問題
今天小編就為大家分享一篇快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08詳解使用angularjs的ng-options時(shí)如何設(shè)置默認(rèn)值(初始值)
本篇文章主要介紹了詳解使用angularjs的ng-options時(shí)如何設(shè)置默認(rèn)值(初始值),具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07Angular4學(xué)習(xí)教程之HTML屬性綁定的方法
這篇文章主要給大家介紹了關(guān)于Angular4學(xué)習(xí)教程之HTML屬性綁定的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01AngularJS實(shí)現(xiàn)tab選項(xiàng)卡的方法詳解
這篇文章主要介紹了AngularJS實(shí)現(xiàn)tab選項(xiàng)卡的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了AngularJS實(shí)現(xiàn)tab選項(xiàng)卡的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07AngularJS實(shí)現(xiàn)的簡(jiǎn)單拖拽功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的簡(jiǎn)單拖拽功能,涉及AngularJS事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請(qǐng)求詳析
這篇文章主要給大家介紹了關(guān)于如何使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用RxJS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼
本篇文章主要介紹了angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06