jQuery implementation div drag effect example analysis

  • 2021-01-02 21:45:04
  • OfStack

This paper analyzes the div drag-and-drop effect of jQuery. To share for your reference, the details are as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<style type="text/css">
/* Drag and drop modules */
.drag{position:absolute;width:100px;height:100px;border:1px solid #ddd;background:#FBF2BD;text-align:center;padding:5px;top:100px;left:20px;cursor:move;}
</style>
<script type="text/javascript">
//  Drag and drop modules 
$(function(){
var _move=false;// Mobile tag 
var _x,_y;// The relative position of the mouse from the upper-left corner of the control 
  $(".drag").click(function(){
    //alert("click");// Click (trigger after release) 
    }).mousedown(function(e){
    _move=true;
    _x=e.pageX-parseInt($(".drag").css("left"));
    _y=e.pageY-parseInt($(".drag").css("top"));
    $(".drag").fadeTo(20, 0.5);// Click to start dragging and display transparently 
  });
  $(document).mousemove(function(e){
    if(_move){
      var x=e.pageX-_x;// The absolute position of the upper left corner of the control is calculated based on the mouse position when moving 
      var y=e.pageY-_y;
      $(".drag").css({top:y,left:x});// Control new position 
    }
  }).mouseup(function(){
  _move=false;
  $(".drag").fadeTo("fast", 1);// After releasing the mouse, stop moving and restore to opacity 
 });
});
</script>
</head>
<body>
<!-- Drag and drop modules --> <div class="drag"> This one can be dragged  ^_^</div>
</body>
</html>


$("#question_pic").bind("click",function(event){
  event.stopPropagation(); // Prevent bubbling event response 
  $("#chat_question").hide("slow");
});


$("#chatLineHolder").scrollTop(10000);
// Keep the bottom 1 Okay, don't roll 

For more information about jQuery, please refer to: Summary of jQuery Drag-and-drop Effects and Techniques, Summary of Common jQuery Effects, Summary of jQuery Extension Techniques, Summary of jquery Selectors and Summary of Common jquery Operation Techniques.

I hope this article has been helpful in jQuery programming.


Related articles: