js的简单拖拽插件,动态创建的div怎么用?

Waitforsniping 2013-03-18 04:55:09
这是所有的代码,网上找的:

(function($)
{
$.extend({
//获取鼠标当前坐标
mouseCoords:function(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
},
//获取样式值
getStyle:function(obj,styleName)
{
return obj.currentStyle ? obj.currentStyle[styleName] : document.defaultView.getComputedStyle(obj,null)[styleName];
// return obj.currentStyle ? obj.currentStyle[styleName] : document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleName);
}
});

// 元素拖拽插件
$.fn.dragDrop = function(options)
{
var opts = $.extend({},$.fn.dragDrop.defaults,options);

return this.each(function(){

//是否正在拖动
var bDraging = false;
//移动的元素
var moveEle = $(this);
//点击哪个元素,以触发移动。
//该元素需要是被移动元素的子元素(比如标题等)
var focuEle = opts.focuEle ? $(opts.focuEle,moveEle) : moveEle ;
if(!focuEle || focuEle.length<=0)
{
alert('focuEle is not found! the element must be a child of '+this.id);
return false;
}
// initDiffX|Y : 初始时,鼠标与被移动元素原点的距离
// moveX|Y : 移动时,被移动元素定位位置 (新鼠标位置与initDiffX|Y的差值)
// 如果定义了移动中的回调函数,该对象将以参数传入回调函数。
var dragParams = {initDiffX:'',initDiffY:'',moveX:'',moveY:''};

//被移动元素,需要设置定位样式,否则拖拽效果将无效。
moveEle.css({'position':'absolute','left':'0','top':'0'});

//点击时,记录鼠标位置
//DOM写法: getElementById('***').onmousedown= function(event);
focuEle.bind('mousedown',function(e){
//标记开始移动
bDraging = true;
//改变鼠标形状
moveEle.css({'cursor':'move'});
//捕获事件。(该用法,还有个好处,就是防止移动太快导致鼠标跑出被移动元素之外)
if(moveEle.get(0).setCapture)
{
moveEle.get(0).setCapture();
}
//(实际上是鼠标当前位置相对于被移动元素原点的距离)
// DOM写法:(ev.clientX + document.body.scrollLeft - document.body.clientLeft) - document.getElementById('***').style.left;
dragParams.initDiffX = $.mouseCoords(e).x - moveEle.position().left;
dragParams.initDiffY = $.mouseCoords(e).y - moveEle.position().top;
});

//移动过程
focuEle.bind('mousemove',function(e){
if(bDraging)
{
//被移动元素的新位置,实际上鼠标当前位置与原位置之差
//实际上,被移动元素的新位置,也可以直接是鼠标位置,这也能体现拖拽,但是元素的位置就不会精确。
dragParams.moveX = $.mouseCoords(e).x - dragParams.initDiffX;
dragParams.moveY = $.mouseCoords(e).y - dragParams.initDiffY;

//是否限定在某个区域中移动.
//fixarea格式: [x轴最小值,x轴最大值,y轴最小值,y轴最大值]
if(opts.fixarea)
{
if(dragParams.moveX<opts.fixarea[0])
{
dragParams.moveX=opts.fixarea[0]
}
if(dragParams.moveX>opts.fixarea[1])
{
dragParams.moveX=opts.fixarea[1]
}

if(dragParams.moveY<opts.fixarea[2])
{
dragParams.moveY=opts.fixarea[2]
}
if(dragParams.moveY>opts.fixarea[3])
{
dragParams.moveY=opts.fixarea[3]
}
}

//移动方向:可以是不限定、垂直、水平。
if(opts.dragDirection=='all')
{
//DOM写法: document.getElementById('***').style.left = '***px';
moveEle.css({'left':dragParams.moveX,'top':dragParams.moveY});
}
else if (opts.dragDirection=='vertical')
{
moveEle.css({'top':dragParams.moveY});
}
else if(opts.dragDirection=='horizontal')
{
moveEle.css({'left':dragParams.moveX});
}

//如果有回调
if(opts.callback)
{
//将dragParams作为参数传递
opts.callback.call(opts.callback,dragParams);
}
}
});

//鼠标弹起时,标记为取消移动
focuEle.bind('mouseup',function(e){
bDraging=false;
moveEle.css({'cursor':'default'});
if(moveEle.get(0).releaseCapture)
{
moveEle.get(0).releaseCapture();
}
});
});
};

//默认配置
$.fn.dragDrop.defaults =
{
focuEle:null, //点击哪个元素开始拖动,可为空。不为空时,需要为被拖动元素的子元素。
callback:null, //拖动时触发的回调。
dragDirection:'all', //拖动方向:['all','vertical','horizontal']
fixarea:null //限制在哪个区域拖动,以数组形式提供[minX,maxX,minY,maxY]
};

})(jQuery);

如果要使用它让某一控件进行拖动操作,只需要添加一句jquery代码:$("id").dragDrop();但是我发现这样只能让已经存在的控件进行拖动。
可是我现在要让动态创建的div使用它进行拖动,求解决方法。
...全文
280 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
李志遠 2013-03-19
  • 打赏
  • 举报
回复
动态绑定试试这个jquery.livequery.js 还有拖拽,这个ui插件里不是都有么
tianma630 2013-03-19
  • 打赏
  • 举报
回复
引用 6 楼 qq542369628 的回复:
引用 5 楼 tianma630 的回复:引用 4 楼 qq542369628 的回复:引用 3 楼 tianma630 的回复:把代码单独放到一个js文件里,然后在文件下面加上一下代码 $(function(){ $(".dragDrop").dragDrop(); }); 在你需要用到的页面引入该js文件,在需要拖动的div加上class="dragD……
用js创建元素?那只能吧$(".dragDrop").dragDrop();放在你创建元素代码的后面 jquery里有个live方法 有这种效果 但是他只支持他定义的那些事件
Waitforsniping 2013-03-19
  • 打赏
  • 举报
回复
引用 5 楼 tianma630 的回复:
引用 4 楼 qq542369628 的回复:引用 3 楼 tianma630 的回复:把代码单独放到一个js文件里,然后在文件下面加上一下代码 $(function(){ $(".dragDrop").dragDrop(); }); 在你需要用到的页面引入该js文件,在需要拖动的div加上class="dragDrop" ,即可我昨天的回复怎么没有了? ……
你还是没看明白啊,我说的是动态创建的div,不是已经存在的,这个当然能用。
tianma630 2013-03-19
  • 打赏
  • 举报
回复
引用 4 楼 qq542369628 的回复:
引用 3 楼 tianma630 的回复:把代码单独放到一个js文件里,然后在文件下面加上一下代码
$(function(){
$(".dragDrop").dragDrop();
});

在你需要用到的页面引入该js文件,在需要拖动的div加上class="dragDrop" ,即可我昨天的回复怎么没有了?
我以前就是这么做的,没有用。
……

刚测试下,这个方法是可以的,实例发给你
js文件 drag.js

(function($)
{
$.extend({
//获取鼠标当前坐标
mouseCoords:function(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
},
//获取样式值
getStyle:function(obj,styleName)
{
return obj.currentStyle ? obj.currentStyle[styleName] : document.defaultView.getComputedStyle(obj,null)[styleName];
// return obj.currentStyle ? obj.currentStyle[styleName] : document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleName);
}
});

// 元素拖拽插件
$.fn.dragDrop = function(options)
{
var opts = $.extend({},$.fn.dragDrop.defaults,options);

return this.each(function(){

//是否正在拖动
var bDraging = false;
//移动的元素
var moveEle = $(this);
//点击哪个元素,以触发移动。
//该元素需要是被移动元素的子元素(比如标题等)
var focuEle = opts.focuEle ? $(opts.focuEle,moveEle) : moveEle ;
if(!focuEle || focuEle.length<=0)
{
alert('focuEle is not found! the element must be a child of '+this.id);
return false;
}
// initDiffX|Y : 初始时,鼠标与被移动元素原点的距离
// moveX|Y : 移动时,被移动元素定位位置 (新鼠标位置与initDiffX|Y的差值)
// 如果定义了移动中的回调函数,该对象将以参数传入回调函数。
var dragParams = {initDiffX:'',initDiffY:'',moveX:'',moveY:''};

//被移动元素,需要设置定位样式,否则拖拽效果将无效。
moveEle.css({'position':'absolute','left':'0','top':'0'});

//点击时,记录鼠标位置
//DOM写法: getElementById('***').onmousedown= function(event);
focuEle.bind('mousedown',function(e){
//标记开始移动
bDraging = true;
//改变鼠标形状
moveEle.css({'cursor':'move'});
//捕获事件。(该用法,还有个好处,就是防止移动太快导致鼠标跑出被移动元素之外)
if(moveEle.get(0).setCapture)
{
moveEle.get(0).setCapture();
}
//(实际上是鼠标当前位置相对于被移动元素原点的距离)
// DOM写法:(ev.clientX + document.body.scrollLeft - document.body.clientLeft) - document.getElementById('***').style.left;
dragParams.initDiffX = $.mouseCoords(e).x - moveEle.position().left;
dragParams.initDiffY = $.mouseCoords(e).y - moveEle.position().top;
});

//移动过程
focuEle.bind('mousemove',function(e){
if(bDraging)
{
//被移动元素的新位置,实际上鼠标当前位置与原位置之差
//实际上,被移动元素的新位置,也可以直接是鼠标位置,这也能体现拖拽,但是元素的位置就不会精确。
dragParams.moveX = $.mouseCoords(e).x - dragParams.initDiffX;
dragParams.moveY = $.mouseCoords(e).y - dragParams.initDiffY;

//是否限定在某个区域中移动.
//fixarea格式: [x轴最小值,x轴最大值,y轴最小值,y轴最大值]
if(opts.fixarea)
{
if(dragParams.moveX<opts.fixarea[0])
{
dragParams.moveX=opts.fixarea[0]
}
if(dragParams.moveX>opts.fixarea[1])
{
dragParams.moveX=opts.fixarea[1]
}

if(dragParams.moveY<opts.fixarea[2])
{
dragParams.moveY=opts.fixarea[2]
}
if(dragParams.moveY>opts.fixarea[3])
{
dragParams.moveY=opts.fixarea[3]
}
}

//移动方向:可以是不限定、垂直、水平。
if(opts.dragDirection=='all')
{
//DOM写法: document.getElementById('***').style.left = '***px';
moveEle.css({'left':dragParams.moveX,'top':dragParams.moveY});
}
else if (opts.dragDirection=='vertical')
{
moveEle.css({'top':dragParams.moveY});
}
else if(opts.dragDirection=='horizontal')
{
moveEle.css({'left':dragParams.moveX});
}

//如果有回调
if(opts.callback)
{
//将dragParams作为参数传递
opts.callback.call(opts.callback,dragParams);
}
}
});

//鼠标弹起时,标记为取消移动
focuEle.bind('mouseup',function(e){
bDraging=false;
moveEle.css({'cursor':'default'});
if(moveEle.get(0).releaseCapture)
{
moveEle.get(0).releaseCapture();
}
});
});
};

//默认配置
$.fn.dragDrop.defaults =
{
focuEle:null, //点击哪个元素开始拖动,可为空。不为空时,需要为被拖动元素的子元素。
callback:null, //拖动时触发的回调。
dragDirection:'all', //拖动方向:['all','vertical','horizontal']
fixarea:null //限制在哪个区域拖动,以数组形式提供[minX,maxX,minY,maxY]
};

})(jQuery);

$(function(){
$(".drag").dragDrop();

});

测试页面 test.html

<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript" src="drap.js"></script>

<script type="text/javascript">

</script>

<div id="drag" class="drag" style="width:50px;height:50px;background:red;">

</div>
<br>
<div id="drag1" class="drag" style="width:50px;height:50px;background:red;">

</div>
Waitforsniping 2013-03-19
  • 打赏
  • 举报
回复
引用 3 楼 tianma630 的回复:
把代码单独放到一个js文件里,然后在文件下面加上一下代码 $(function(){ $(".dragDrop").dragDrop(); }); 在你需要用到的页面引入该js文件,在需要拖动的div加上class="dragDrop" ,即可
我昨天的回复怎么没有了? 我以前就是这么做的,没有用。
tianma630 2013-03-18
  • 打赏
  • 举报
回复
把代码单独放到一个js文件里,然后在文件下面加上一下代码 $(function(){ $(".dragDrop").dragDrop(); }); 在你需要用到的页面引入该js文件,在需要拖动的div加上class="dragDrop" ,即可
Waitforsniping 2013-03-18
  • 打赏
  • 举报
回复
不会改啊。
bichir 2013-03-18
  • 打赏
  • 举报
回复
把这个写成一个调用涵数,动态创建好后 再手动调用这个涵数执行一下

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧