成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? jQuery實現div橫向拖拽排序的簡單實例
全站頭部文字 我要出現在這里
jQuery實現div橫向拖拽排序的簡單實例 648 2024-02-25   

實例如下:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>div橫向拖拽排序</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<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 type="text/javascript">
  $(document).ready( function () { 
var range = { 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;
  //鼠標元素相對偏移量 
  range.x = event.pageX - theDiv.offset().left; 
  range.y = event.pageY - theDiv.offset().top; 
  theDivId = theDiv.index(); 
  theDivWidth = theDiv.width(); 
  theDivHalf = 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(event) { 
if(!choose){return false;}
  if(!move){
  theDiv.attr("class", "horizontal-div");
  tempDiv.remove(); // 刪除新建的虛線div
  choose = false;
   return false;
  }
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 虛線div的位置上 
theDiv.attr("class", "horizontal-div"); //恢復對象的初始樣式 
tempDiv.remove(); // 刪除新建的虛線div 
move = false;
choose = false;
  }).mousemove(function(event) { 
  if(!choose) return false;
  move = true;
lastPos.x = event.pageX - range.x; 
lastPos.y = event.pageY - range.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 + theDivHalf ; // 第一個元素對象的中心縱坐標 
  //拖拽對象 移動到第一個位置 
  if (lastPos.x <= tarFirstX) { 
  tempDiv.insertBefore(tarFirst); 
  } 
  //判斷要插入目標元素的 坐標后, 直接插入 
  if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1 ) { 
  tempDiv.insertAfter(tarDiv); 
  } 
  }); 
  });


  });
</script>
</head>
<body>
<h1>div橫向拖拽排序</h1>
<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>
</body>
</html>

以上這篇jQuery實現div橫向拖拽排序的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持綠夏網。



主站蜘蛛池模板: 成毅壁纸| 色戒在线视频观看| 自拍性视频| 日韩电影免费观看高清完整版在线| 鲁滨逊漂流记阅读笔记| 龙的新娘电视剧全集| 张俪写真集照片| nhk| 月亮电影| 龙之战电影| 艳妇乳肉豪妇荡乳ⅹxxo| 赵芮| 珠帘玉幕图片| 最佳嫌疑人电影免费观看| 彭丹丹主演的经典电影| 在线免费污视频| 张剑虹| 中华英雄何润东| 辩论赛作文| 红海行动2虎鲸行动| 韩国电影《表妹》| 松永纱奈| 五年级上册书法教案| 情侣自拍偷拍| 叶玉卿电影| 河北卫视节目表| 小组介绍| 少妇av片在线观看| 闺房趣事电影| 巨齿鲨2:深渊 电影| 电影英雄| a型血和b型血生的孩子是什么血型| angela white在线播放| 西藏秘密演员表全部| 天下第一楼演员表全部| 子宫前壁和子宫后壁的区别| 巧巧| 小数乘小数计算100道题| 落鞠婧祎| 金珠和陈诗雅主演的韩剧| 雷雨剧本完整版|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103