当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery拖拽排序的实现教程

jQuery拖拽排序的实现教程

2018年10月10日  | 移动技术网IT编程  | 我要评论
修改了原文章一个致命bug,以及删除了认为多余的代码,下一步就是实现数据异步提交了 <!doctype html> <html> <head>

修改了原文章一个致命bug,以及删除了认为多余的代码,下一步就是实现数据异步提交了

<!doctype html>  
<html>  
<head>  
<meta http-equiv="content-type" content="text/html; charset=utf-8" />  
<title>jquery拖拽</title>  
<style type="text/css">  
body, p { margin: 0; paading: 0; font-size: 12px;user-select: none;}  
body { width: 960px; margin: 0 auto; }  
ul, li { margin: 0; padding: 0; list-style: none; }  
.clear { clear: both; width: 1px; height: 0px; line-height: 0px; font-size: 1px; }  
  
.box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; }  
.main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; }  
.maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; }  
  
.dash { width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; display:block;};  
</style>  
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>  
<script type="text/javascript">  
  
$(document).ready( function () {  
  
    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;//拖拽对象 拖拽状态  
    var thedivheight = 0, tarfirsty = 0; //拖拽对象的索引、高度、的初始化。  
    var tardiv = null, tarfirst = null;  //要插入的目标元素的对象, 临时的虚线对象  
    var tempdiv =  $("<p class='dash'></p>"); //虚线框对象  
      
        $(".main").mousedown(function (event){  
            //拖拽对象  
            thediv = $(this);  
             
            thedivheight = thediv.height();  
            move = true;  
            thediv.attr("class","maindash");  
              
            thediv.css({top: event.pagey-thedivheight/2});  
            tempdiv.insertbefore(thediv);  
                     
        });  
      
     
    $(document).mousemove(function(event) {  
     
        if (!move) return false;  
  
        lastpos.y = event.pagey - thedivheight/2;  
        lastpos.y1 = lastpos.y + thedivheight;  
         
        // 拖拽元素随鼠标移动  
        thediv.css({top: lastpos.y});  
        // 拖拽元素随鼠标移动 查找插入目标元素  
         
        var  $main = $('.main'); // 局部变量:按照重新排列过的顺序  再次获取 各个元素的坐标,  
         
        $main.each(function () {  
            tardiv = $(this);  
            tarpos.x = tardiv.offset().left;  
            tarpos.y = tardiv.offset().top;  
            tarpos.y1 = tarpos.y + tardiv.height()/2;  
             
            tarfirst = $main.eq(0); // 获得第一个元素  
            tarfirsty = tarfirst.offset().top + thedivheight/2 ; // 第一个元素对象的中心纵坐标  
             
            //拖拽对象 移动到第一个位置  
            if (lastpos.y <= tarfirsty) {  
                    tempdiv.insertbefore(tarfirst);  
            }  
            //判断要插入目标元素的 坐标后, 直接插入  
            if (lastpos.y >= tarpos.y - thedivheight/2 && lastpos.y1 >= tarpos.y1 ) {  
                tempdiv.insertafter(tardiv);  
            }  
             
        });  
  
    }).mouseup(function(event) {  
         
        thediv.insertbefore(tempdiv);  // 拖拽元素插入到 虚线p的位置上  
        thediv.attr("class", "main"); //恢复对象的初始样式  
        thediv =null; //用完的对象记得释放  
        tempdiv.remove(); // 删除新建的虚线p  
        move=false;  
         
    });  
     
});  
  
  
</script>  
</head>  
  
<body>   
<p class="box" id="box">  
    <p class="main" id="main0">p1</p>  
    <p class="main" id="main1">p2</p>  
    <p class="main" id="main2">p3</p>  
    <p class="main" id="main3">p4</p>  
    <p class="main" id="main4">p5</p>  
</p>  
</body>  
</html>  

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网