码迷,mamicode.com
首页 > 编程语言 > 详细

拖拽排序

时间:2017-10-13 12:41:08      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:das   false   状态   top   att   判断   after   src   box   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>div的横向排序</title>
<style type="text/css">
body,div{padding: 0px;margin: 0px;}
.box{margin-left: 15px;padding : 10px;padding-right: 0px;width : 810px;height : 150px;border : blue solid 1px;}

.horizontal-div{
float : left;
margin-right: 10px;
border: #000 solid 1px;
text-align: center;
width : 150px;
height : 150px;
}
.horizontal-div-dash {
position: absolute;
width: 150px;
height: 150px;
margin-right: 10px;
border: 1px dashed blue;
background: #ececec;
opacity: 0.7;
}

.dash {
float : left;
width: 150px;
height: 150px;
margin-right: 10px;
border: 1px dashed #f00;
};

</style>
<script src="js/jquery1.7.js"></script>
</head>
<body>
<div class="box">
<div class="horizontal-div" id="div1">div1</div>
<div class="horizontal-div" id="div2">div2</div>
<div class="horizontal-div" id="div3">div3</div>
<div class="horizontal-div" id="div4">div4</div>
<div class="horizontal-div" id="div5">div5</div>
</div>

<script>
$(document).ready(function(){
var MouseOffset={x:0,y:0}//鼠标的相对偏移量;
var lastPos={x:0,y:0,x1:0,y1:0} //拖拽对象的四个坐标
var tarPos={x:0,y:0,x1:0,y1:0} //目标对象的四个坐标
var theDiv = null, move = false; choose = false; //拖拽对象 拖拽状态 选中状态
var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽对象的索引、高度、的初始化。
var tarDiv = null, tarFirst, tempDiv; //要插入的目标元素的对象, 临时的虚线对象
var initPos = {x: 0, y: 0};
$(‘.horizontal-div‘).each(function(){
$(this).mousedown(function(event){
choose=true;
theDiv=$(this);
//初始元素的位置
initPos.x=theDiv.offset().left;
initPos.y=theDiv.offset().top;
//鼠标的相对偏移量
MouseOffset.x=event.clientX-initPos.x;
MouseOffset.y=event.clientY-initPos.y;
theDivInd=theDiv.index()//当前元素的下标;
theDivWidth=theDiv.width()//当前元素的宽度
theDivWidthHalf=theDivWidth/2;
theDiv.attr("class","horizontal-div-dash");
theDiv.css({"left":initPos.x+"px","top":initPos.y+"px"});
//创建一元素,把元素插入到当前元素的前面(蓝色虚线元素)
$(‘<div class="dash"></div>‘).insertBefore(theDiv);//红色
tempDiv = $(".dash");

});
})
$(document).mouseup(function(){
if(!choose)return false;
if(!move){
tempDiv.remove();
theDiv.attr("class","horizontal-div");
choose=false;
return false;
}
theDiv.insertBefore(tempDiv);// 拖拽元素插入到 虚线div的位置上
theDiv.attr("class","horizontal-div");
tempDiv.remove();
move = false;
choose = false;

}).mousemove(function(){
if(!choose) return false;
move = true;
lastPos.x = event.pageX - MouseOffset.x;
lastPos.y = event.pageY - MouseOffset.y;
lastPos.x1 = lastPos.x + theDivWidth;
// 拖拽元素随鼠标移动
theDiv.css({left: lastPos.x + ‘px‘,top: lastPos.y + ‘px‘});
// 拖拽元素随鼠标移动 查找插入目标元素
var $main = $(‘.horizontal-div‘); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标,
$main.each(function () {
tarDiv = $(this);
tarPos.x = tarDiv.offset().left;
tarPos.y = tarDiv.offset().top;
tarPos.x1 = tarPos.x + tarDiv.width()/2;
tarFirst = $main.eq(0); // 获得第一个元素
tarFirstX = tarFirst.offset().left + theDivWidthHalf ; // 第一个元素对象的中心纵坐标
//拖拽对象 移动到第一个位置
if (lastPos.x <= tarFirstX) {
tempDiv.insertBefore(tarFirst);
}
//判断要插入目标元素的 坐标后, 直接插入
if (lastPos.x >= tarPos.x - theDivWidth && lastPos.x1 >= tarPos.x1 ) {
tempDiv.insertAfter(tarDiv);
}
});
});
});
</script>
</body>
</html>

拖拽排序

标签:das   false   状态   top   att   判断   after   src   box   

原文地址:http://www.cnblogs.com/y-mj/p/7660248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!