AngularJS實現(xiàn)的簡單拖拽功能示例
更新時間:2018年01月02日 12:20:48 作者:Web攻城獅
這篇文章主要介紹了AngularJS實現(xiàn)的簡單拖拽功能,涉及AngularJS事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了AngularJS實現(xiàn)的簡單拖拽功能。分享給大家供大家參考,具體如下:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>www.fzitv.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">
<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>
運行效果如下:

更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結(jié)》、《AngularJS入門與進階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對大家AngularJS程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于AngularJS中幾種Providers的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于AngularJS中幾種Providers的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用AngularJS具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁面問題詳解
這篇文章主要給大家介紹了angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁面問題的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05

