熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

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

2013-11-15 12:05:22  來源: JSP教程 

  以前用原生的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); 
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20266.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.