javascript

位置:IT落伍者 >> javascript >> 浏览文章

div拖拽插件——JQ.MoveBox.js


发布日期:2021年07月15日
 
div拖拽插件——JQ.MoveBox.js

以前用原生的JS做过类似拖拽div的效果现在按原思路改做成一个JQ的小插件当作制作JQ插件的一个小练习
html为

代码如下:
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head>
<meta httpequiv="ContentType" content="text/html;charset=gb" />
<title></title>
<style type="text/css">
*{margin:;padding:;}
#box{width:px;height:px;margin:px auto;position:relative;border:px solid #ccc;borderleft:px solid #ccc;}
floatbox{width:px;height:px;background:#;color:#fff;position:absolute;top:px;left:px;cursor:move;zindex:;border:px solid #ccc;borderright:px solid #fc;}
floatbox{width:px;height:px;background:#f;color:#fff;position:absolute;top:;left:px;cursor:move;bordertop:px solid #;}
</style>
</head>
<body>
<div id="box">
<div class="floatbox"></div>
<div class="floatbox"></div>
</div>
<script type="text/javascript" src="
<script type="text/javascript" src="JQMoveBoxjs"></script>
<script type="text/javascript">
$("floatbox")MoveBox();
$("floatbox")MoveBox({out:true});
</script>
</body>
</html>


下面为JQMoveBoxjs

代码如下:
(function($){
var n = ;
var o = {}
$fnMoveBox=function(options){
var opts = $extend({} $fnMoveBoxdefaults options);
return thiseach(function(i){
$(this)mousedown(function(e){
oiTop = $(this)position()top epageY;
oiLeft = $(this)position()left epageX;
n++;
$this = $(this);
$thiscss({zindex:n});
$(document)bind("mousemove"function(e){
var iLeft = epageX + oiLeft;
var iTop = epageY + oiTop;
if(optsout){
if(iLeft<$thisparent()offset()leftparseInt($thisparent()css("borderleftwidth"))){
iLeft = $thisparent()offset()leftparseInt($thisparent()css("borderleftwidth"));
}else if(iLeft>$(document)width()$thiswidth()parseInt($thiscss("borderleftwidth"))parseInt($thiscss("borderrightwidth"))$thisparent()offset()leftparseInt($thisparent()css("borderleftwidth"))){
iLeft = $(document)width()$thiswidth()parseInt($thiscss("borderleftwidth"))parseInt($thiscss("borderrightwidth"))$thisparent()offset()leftparseInt($thisparent()css("borderleftwidth"));
}
if(iTop<$thisparent()offset()topparseInt($thisparent()css("bordertopwidth"))+$(document)scrollTop()){
iTop = $thisparent()offset()topparseInt($thisparent()css("bordertopwidth"))+$(document)scrollTop();
}else if(iTop>$(window)height()+$(document)scrollTop()$thisheight()parseInt($thiscss("bordertopwidth"))parseInt($thiscss("borderbottomwidth"))$thisparent()offset()topparseInt($thisparent()css("bordertopwidth"))){
iTop = $(window)height()+$(document)scrollTop()$thisheight()parseInt($thiscss("bordertopwidth"))parseInt($thiscss("borderbottomwidth"))$thisparent()offset()topparseInt($thisparent()css("bordertopwidth"));
}
}else{
if(iLeft<){
iLeft = ;
}else if(iLeft>$thisparent()width()$thiswidth()parseInt($thiscss("borderleftwidth"))parseInt($thiscss("borderrightwidth"))){
iLeft = $thisparent()width()$thiswidth()parseInt($thiscss("borderleftwidth"))parseInt($thiscss("borderrightwidth"));
}
if(iTop<){
iTop = ;
}else if(iTop>$thisparent()height()$thisheight()parseInt($thiscss("bordertopwidth"))parseInt($thiscss("borderbottomwidth"))){
iTop = $thisparent()height()$thisheight()parseInt($thiscss("bordertopwidth"))parseInt($thiscss("borderbottomwidth"));
}
}
$thiscss({
left:iLeft +"px"
top:iTop + "px"
})
});
$(document)bind("mouseup"function(){
$(document)unbind("mousemove");
$(document)unbind("mouseup");
});
});
});
};

$fnMoveBoxdefaults = {
out:false //默认不可跑出线外
};
})(jQuery);

上一篇:Jquery优化技巧

下一篇:javascript实现文字图片上下滚动的具体实例