基于jquery的拖动布局插件 - Web前端
作者:98wpeu发布时间:2026-07-02分类:网页前端技术浏览:5
导读:复制代码代码如下:(function($){$.fn.lsMovePanel=function(){varid=this.attr("id");varX=Y=0;va...
复制代码 代码如下:
(function($){
$.fn.lsMovePanel=function(){
var id=this.attr("id");
var X=Y=0;
var offsetX=offsetY=0;//绝对位置
var OldIndex=0;///存储原始索引
var Temp_Li="<li id=\"Temp_Li\" style=\"background-color:#FFFFFF;border-color:#FF023C\"></li>";
var Move_obj;///当前拖动的对象
$("#"+id+" li").each(function(i){
$(this).attr("open","0");
//鼠标点击
$(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);
}
})
}
});
//鼠标放开
$(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"
});
});
}
});
//移动
$(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"
});
//判断覆盖对象索引值在前还是后
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);
调用例子:
复制代码 代码如下:
<!DOCtypehtml 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>
(function($){
$.fn.lsMovePanel=function(){
var id=this.attr("id");
var X=Y=0;
var offsetX=offsetY=0;//绝对位置
var OldIndex=0;///存储原始索引
var Temp_Li="<li id=\"Temp_Li\" style=\"background-color:#FFFFFF;border-color:#FF023C\"></li>";
var Move_obj;///当前拖动的对象
$("#"+id+" li").each(function(i){
$(this).attr("open","0");
//鼠标点击
$(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);
}
})
}
});
//鼠标放开
$(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"
});
});
}
});
//移动
$(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"
});
//判断覆盖对象索引值在前还是后
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);
调用例子:
复制代码 代码如下:
<!DOCtypehtml 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>
相关推荐
- 基于jquery的代码显示区域自动拉长效果 - Web前端
- 鼠标拖动实现DIV排序示例代码 - Web前端
- jBox 2.3基于jquery的最新多功能对话框插件 常见使用问题解答 - Web前端
- jQuery EasyUI API 中文文档 - NumberSpinner数值微调器使用介绍 - Web前端
- jquery ajax return没有返回值的解决方法 - Web前端
- Jquery Change与bind事件代码 - Web前端
- jquery 查找iframe父级页面元素的实现代码 - Web前端
- Jquery 表格合并的问题分享 - Web前端
- 精心收集的jQuery常用的插件1000 - Web前端
- 精心收集的jQuery常用的插件1000 - Web前端
- 网页前端技术排行
-
- 1【第六章】Foundation之按钮和下拉功能 - Web前端
- 2jQuery编写widget的一些技巧分享 - Web前端
- 3在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 4基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 5分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 9JS网页制作实例:标签云 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


