基于jquery的拖動(dòng)布局插件
更新時(shí)間:2011年11月25日 01:09:08 作者:
基于jquery的拖動(dòng)布局插件,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
(function($){
$.fn.lsMovePanel=function(){
var id=this.attr("id");
var X=Y=0;
var offsetX=offsetY=0;//絕對(duì)位置
var OldIndex=0;///存儲(chǔ)原始索引
var Temp_Li="<li id=\"Temp_Li\" style=\"background-color:#FFFFFF;border-color:#FF023C\"></li>";
var Move_obj;///當(dāng)前拖動(dòng)的對(duì)象
$("#"+id+" li").each(function(i){
$(this).attr("open","0");
//鼠標(biāo)點(diǎn)擊
$(this).bind("mousedown",function(){
if(event.button==1 || event.button==0){$(this).attr("open","1");}
if($(this).attr("open")=="1"){
$(this).css({
cursor:"move",
opacity:"0.7"
});
X=event.clientX;
Y=event.clientY;
offsetX=$(this).offset().left;
offsetY=$(this).offset().top;
OldIndex=$(this).index();
$(this).css({
position:"absolute",
left:offsetX,
top:offsetY
});
$("#"+id+" li").each(function(i){
if(i==OldIndex){
$(this).after(Temp_Li);
}
})
}
});
//鼠標(biāo)放開
$(this).bind("mouseup",function(){
if(event.button==1 || event.button==0){$(this).attr("open","0");}
if($(this).attr("open")=="0"){
$("#Temp_Li").before($(this));
$(this).animate({
left:$("#Temp_Li").offset().left,
top:$("#Temp_Li").offset().top,
},300,function(){
$("#Temp_Li").remove();
$(this).css({
cursor:"default",
opacity:"1",
position:"static"
});
});
$("#"+id+" li").each(function(i){
$(this).css({
"border-color":"#666666"
});
});
}
});
//移動(dòng)
$(this).bind("mousemove",function(){
if($(this).attr("open")=="1"){
var current_X=current_Y=0;
current_X=offsetX+event.clientX-X;
current_Y=offsetY+event.clientY-Y;
$(this).css({
position:"absolute",
left:current_X,
top:current_Y
});
Move_obj=this;
$("#"+id+" li").each(function(i){
if(i!=OldIndex && $(this).attr("id")!="Temp_Li"){
var Deviation=0;
var Max_X=$(this).offset().left+$(this).width()-Deviation;
var Min_X=$(this).offset().left+Deviation;
var Max_Y=$(this).offset().top+$(this).height()-Deviation;
var Min_Y=$(this).offset().top+Deviation;
if((event.clientX < Max_X) && (event.clientY+$(Move_obj).height() > Max_Y) && (event.clientY+$(Move_obj).height() > Min_Y) && (event.clientX > Min_X) && (event.clientY < Max_Y) ){
$(this).css({
"border-color":"#FF7578"
});
//判斷覆蓋對(duì)象索引值在前還是后
if(OldIndex>$(this).index()){
$("#Temp_Li").before($(this));
$("#Temp_Li").remove();
$(this).before(Temp_Li);
}else{
$("#Temp_Li").after($(this));
$("#Temp_Li").remove();
$(this).after(Temp_Li);
}
}else{
$(this).css({
"border-color":"#666666"
});
}
}
})
}
});
});
}
})(jQuery);
調(diào)用例子:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title></title>
<style>
#Panel{
width:630px;
height:auto;
padding:0px;
}
#Panel li{
float:left;
list-style:none;
width:300px;
height:100px;
margin:5px;
background-color:#D9F1FF;
border:1px dotted #666666;
text-align:center; position:static;
}
*{
font-size:12px;
}
</style>
</head>
<script src="http://demo.jb51.net/jslib/jquery/jquery-1.4.2.min.js"></script>
<script src="http://demo.jb51.net/jslib/lsMovePanel.js"></script>
<body>
<div style="margin-left:100px;">
<ul id="Panel">
<li style="background-color:#3399FF"></li>
<li style="background-color:#00CCFF"></li>
<li style="background-color:#CC9900"></li>
<li style="background-color:#FF6600"></li>
<li style="background-color:#FFCC99"></li>
</ul>
</div>
<script>
$("#Panel").lsMovePanel();
</script>
</body>
</html>
您可能感興趣的文章:
- PHP+MySQL+jQuery隨意拖動(dòng)層并即時(shí)保存拖動(dòng)位置實(shí)例講解
- PHP+jQuery實(shí)現(xiàn)隨意拖動(dòng)層并即時(shí)保存拖動(dòng)位置
- jQuery 拖動(dòng)層(在可視區(qū)域范圍內(nèi))
- 使用jQuery的easydrag插件實(shí)現(xiàn)可拖動(dòng)的DIV彈出框
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)拖動(dòng)基本操作
- jQuery插件slider實(shí)現(xiàn)拖動(dòng)滑塊選取價(jià)格范圍
- jquery拖動(dòng)插件(jquery.drag)使用介紹
- jquery拖動(dòng)層效果插件用法實(shí)例分析(附demo源碼)
相關(guān)文章
使用jquery.qrcode.min.js實(shí)現(xiàn)中文轉(zhuǎn)化二維碼
本文給大家介紹的是一款jQuery的插件jquery.qrcode.min.js,其主要功能是可以生成二維碼,而且可以完美支持中文,下面就給小伙伴們介紹下他的用法2016-03-03
jQuery+Datatables實(shí)現(xiàn)表格批量刪除功能【推薦】
這篇文章主要介紹了jQuery+Datatables實(shí)現(xiàn)表格批量刪除功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
JQuery+CSS提示框?qū)崿F(xiàn)思路及代碼(純手工打造)
純手工打造、兼容性還哦可、可移植任何項(xiàng)目感興趣的朋友可以學(xué)習(xí)下,希望對(duì)你的jquery提升有所幫助2013-05-05
jQuery處理xml格式的返回?cái)?shù)據(jù)(實(shí)例解析)
在以下示例中,我將使用$.ajax()方法,使用$.get()方法也可以,但我覺(jué)得$.ajax()更好一些,代碼更容易懂,并且也不怎么復(fù)雜2013-11-11
如何將 jQuery 從你的 Bootstrap 項(xiàng)目中移除(取而代之使用Vue.js)
Bootstrap是網(wǎng)上最流行的前端開發(fā)框架。下面通過(guò)本文給大家分享將 jQuery 從你的 Bootstrap 項(xiàng)目中移除的方法,需要的的朋友參考下吧2017-07-07
jQuery實(shí)現(xiàn)字符串按指定長(zhǎng)度加入特定內(nèi)容的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)字符串按指定長(zhǎng)度加入特定內(nèi)容的方法,實(shí)例分析了jQuery操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
基于JQuery的動(dòng)態(tài)刪除Table表格的行和列的代碼
基于JQuery的動(dòng)態(tài)刪除Table表格的行和列的代碼以前腳本之家也發(fā)布過(guò)相關(guān)的代碼,大家可以參考下。2011-05-05

