A Simple Example of jQuery Realizing div Horizontal Drag Sorting

  • 2021-07-04 17:39:26
  • OfStack

Examples are as follows:


<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>div Horizontal drag sort </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 };// Mouse element offset  
var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; // Object of the drag-and-drop object 4 Coordinate  
var tarPos = { x: 0, y: 0, x1: 0, y1: 0 }; // Coordinate initialization of the target element object  
var theDiv = null, move = false; choose = false; // Drag object   Drag state   Selected status 
var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; // Drag and drop the initialization of the index, height and height of the object.  
var tarDiv = null, tarFirst, tempDiv; // The object of the target element to insert ,  Temporary dashed line object  
var initPos = {x: 0, y: 0};
$(".horizontal-div").each(function(){ 
  $(this).mousedown(function (event){ 
choose = true;
  // Drag object  
  theDiv = $(this); 
  // Record the initial position of the drag element 
  initPos.x = theDiv.offset().left;
  initPos.y = theDiv.offset().top;
  // Relative offset of mouse elements  
  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'}); 
   //  Create a new element   Position before inserting drag element ( Dashed box ) 
  $("<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(); //  Delete the newly created dotted line div
  choose = false;
   return false;
  }
theDiv.insertBefore(tempDiv); //  Drag and drop elements to insert into   Dashed line div On the position of  
theDiv.attr("class", "horizontal-div"); // Restore the original style of the object  
tempDiv.remove(); //  Delete the newly created dotted line 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; 
//  Drag elements to move with the mouse  
theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'}); 
//  Drag elements to move with the mouse   Find the insertion target element  
var $main = $('.horizontal-div'); //  Local variables: in rearranged order   Retrieve again   The coordinates of each element,  
$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); //  Get the first 1 Elements  
  tarFirstX = tarFirst.offset().left + theDivHalf ; //  No. 1 1 The center ordinate of an element object  
  // Drag object   Move to the 1 Position  
  if (lastPos.x <= tarFirstX) { 
  tempDiv.insertBefore(tarFirst); 
  } 
  // Object to insert the target element   After the coordinates,   Direct insertion  
  if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1 ) { 
  tempDiv.insertAfter(tarDiv); 
  } 
  }); 
  });


  });
</script>
</head>
<body>
<h1>div Horizontal drag sort </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>

Related articles: