jQuery implements div horizontal drag and drop sorting

Reference: https://blog.csdn.net/kongjiea/article/details/45578033

Rendering:

 

html

<h1>div horizontal drag sorting</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>  

 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;   
                        };   

 

js

$(document).ready( function () {   
                        var range = { x: 0, y: 0 };//Mouse element offset   
                        var lastPos = { x: 0, y: 0, x1: 0, y1: 0 }; //The four coordinates of the dragged object   
                        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 state  
                        var theDivId =0, theDivHeight = 0, theDivHalf = 0; tarFirstY = 0; //Initialization of the index, height, and dragging object.   
                        var tarDiv = null, tarFirst, tempDiv; //The object of the target element to be inserted, a temporary dashed 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 dragged element  
                                    initPos.x = theDiv.offset().left;  
                                    initPos.y = theDiv.offset().top;  
                                    //relative offset of mouse element   
                                    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 and insert the position before the dragged 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(); // remove the newly created dashed div  
                                                    choose = false;  
                                                     return false;  
                                            }  
                                        theDiv.insertBefore(tempDiv); // Insert the drag element to the position of the dotted div   
                                        theDiv.attr("class", "horizontal-div"); //Restore the initial style of the object   
                                        tempDiv.remove(); // remove the newly created dashed 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 the element to move with the mouse   
                                                theDiv.css({left: lastPos.x + 'px',top: lastPos.y + 'px'});   
                                                // The drag element moves with the mouse to find the insertion target element   
                                                var $main = $('.horizontal-div'); // Local variables: get the coordinates of each element again in the rearranged order,   
                                                $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 element   
                                                    tarFirstX = tarFirst.offset().left + theDivHalf ; // center ordinate of the first element object   
                                                    // Drag the object to move to the first position   
                                                    if (lastPos.x <= tarFirstX) {   
                                                                   tempDiv.insertBefore(tarFirst);   
                                                    }   
                                                    //After judging the coordinates of the target element to be inserted, insert directly   
                                                    if (lastPos.x >= tarPos.x - theDivHalf && lastPos.x1 >= tarPos.x1 ) {   
                                                            tempDiv.insertAfter(tarDiv);   
                                                    }   
                                            });   
                                    });  
  
  
                    });  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325083697&siteId=291194637