最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)動態(tài))

 更新時(shí)間:2017年02月09日 14:13:58   作者:bayaci  
這篇文章主要介紹了簡單的jQuery拖拽排序效果的實(shí)現(xiàn)(增強(qiáng)),增強(qiáng)動態(tài)增加div效果,代碼簡單,很容易實(shí)現(xiàn),需要的朋友可以參考下

增強(qiáng)動態(tài)增加Div效果

原來沒有新建動作,分析代碼后發(fā)現(xiàn)很容易增強(qiáng)~~

<!DOCTYPE HTML> 
 <html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 <title>測試的拖拽功能</title> 
 <style type="text/css"> 
 body, div { margin: 0; paading: 0; font-size: 12px; } 
 body { width:100%; 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; } 
 .drag_module_box { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } 
 .drag_module_box1 { width: 600px; height: auto; margin: 25px 0 0 0; padding: 5px; border: 1px solid #f00; } 
 .drag_module_main { position: static; width: 600px; height: 80px; margin-bottom: 5px; border: 1px solid blue; background: #ccc; } 
 .drag_module_maindash { position: absolute; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed blue; background: #ececec; opacity: 0.7; } 
 .drag_module_hide { width: 600px; height: 80px; margin-bottom: 5px; } 
 .drag_module_dash { position: sta;tic; width: 600px; height: 80px; margin-bottom: 5px; border: 1px dashed #f00; }; 
 </style> 
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
 <script type="text/javascript"> 
 $(document).ready( function () { 
  //來源:http://www.cnblogs.com/web-ed2/archive/2011/09/19/2181819.html 
   var range = { x: 0, y: 0 };//鼠標(biāo)元素偏移量 
   var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //拖拽對象的四個(gè)坐標(biāo) 
   var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; //目標(biāo)元素對象的坐標(biāo)初始化 
   var theDiv = null, move = false;//拖拽對象 拖拽狀態(tài) 
   var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //拖拽對象的索引、高度、的初始化。 
   var tarDiv = null, tarFirst, tempDiv; //要插入的目標(biāo)元素的對象, 臨時(shí)的虛線對象 
  function loopbox(){ //循環(huán)初始化 
     $(".drag_module_box").find(".drag_module_main").each(function(){ 
      console.log( 'find' ); 
       $(this).mousedown(function (event){ 
         //拖拽對象 
         theDiv = $(this); 
         //鼠標(biāo)元素相對偏移量 
         range.x = event.pageX - theDiv.offset().left; 
         range.y = event.pageY - theDiv.offset().top; 
         theDivId = theDiv.index(); 
         theDivHeight = theDiv.height(); 
         theDivHalf = theDivHeight/2; 
         move = true; 
         theDiv.attr("class","drag_module_maindash"); 
         // 創(chuàng)建新元素 插入拖拽元素之前的位置(虛線框) 
         $("<div class='drag_module_dash'></div>").insertBefore(theDiv); 
       }); 
     }); 
  } 
  loopbox(); 
   $(".drag_module_box").mousemove(function(event) { 
    console.log( 'mousemove' ); 
     if (!move) return false; 
     lastPos.x = event.pageX - range.x; 
     lastPos.y = event.pageY - range.y; 
     lastPos.y1 = lastPos.y + theDivHeight; 
     // 拖拽元素隨鼠標(biāo)移動 
     theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'}); 
     // 拖拽元素隨鼠標(biāo)移動 查找插入目標(biāo)元素 
     var $main = $('.drag_module_main'); // 局部變量:按照重新排列過的順序 再次獲取 各個(gè)元素的坐標(biāo), 
     tempDiv = $(".drag_module_dash"); //獲得臨時(shí) 虛線框的對象 
     $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); // 獲得第一個(gè)元素 
       tarFirstY = tarFirst.offset().top + theDivHalf ; // 第一個(gè)元素對象的中心縱坐標(biāo) 
       //拖拽對象 移動到第一個(gè)位置 
       if (lastPos.y <= tarFirstY) { 
           tempDiv.insertBefore(tarFirst); 
       } 
       //判斷要插入目標(biāo)元素的 坐標(biāo)后, 直接插入 
       if (lastPos.y >= tarPos.y - theDivHalf && lastPos.y1 >= tarPos.y1 ) { 
         tempDiv.insertAfter(tarDiv); 
       } 
     }); 
   }).mouseup(function(event) { 
    console.log( 'mouseup' ); 
    if(theDiv==null) return false; 
     theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上 
     theDiv.attr("class", "drag_module_main"); //恢復(fù)對象的初始樣式 
     $('.drag_module_dash').remove(); // 刪除新建的虛線div 
     move=false; 
   }); 
   $("#drag_module_insert").click(function(){ 
    $("#drag_module_box1").html($("#drag_module_box1").html()+$("#drag_module_box2").html()); 
    loopbox(); 
   }); 
   $("#drag_module_seque").click(function(){ 
    $(".drag_module_box").find(".drag_module_main").each(function(){ 
      console.log($(this).attr('id')); 
    }); 
   }); 
 }); 
 </script> 
 </head> 
 <body>  
 <div class="drag_module_box" id="drag_module_box1"> 
   <div class="drag_module_main" id="main1">div1</div> 
   <div class="drag_module_main" id="main2">div2</div> 
   <div class="drag_module_main" id="main3">div3</div> 
   <div class="drag_module_main" id="main4">div4</div> 
   <div class="drag_module_main" id="main5">div5</div> 
   <div class="drag_module_main" id="main6">div6</div> 
 </div> 
 <div class="drag_module_box1" id="drag_module_box2"> 
  <div class="drag_module_main" id="main_first">div7</div> 
 </div> 
 <input type="button" value="新建" id="drag_module_insert"/> 
 <input type="button" value="確定" id="drag_module_seque"/> 
 </body> 
 </html> 

相關(guān)文章

最新評論

博罗县| 温宿县| 利川市| 南召县| 青浦区| 卢湾区| 瑞金市| 广昌县| 长寿区| 威信县| 奉节县| 万山特区| 汉中市| 吴旗县| 合阳县| 万宁市| 儋州市| 八宿县| 通渭县| 深圳市| 新民市| 合水县| 祁东县| 海伦市| 康马县| 丹阳市| 黔西| 犍为县| 内黄县| 宣武区| 揭西县| 台东县| 宿州市| 阆中市| 都安| 济源市| 外汇| 兴国县| 怀柔区| 文成县| 波密县|