This article describes the mouse drag animated rectangle box implemented by jQuery. Share it for your reference, as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml" xml:lang="zh" lang="zh"> <head> <title>Mouse drag animated rectangle</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <script src="/jquery/2.0.0/"></script> <style type="text/css"> body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td {margin: 0;padding: 0} html {color: #000;overflow-y: scoll;overflow: -moz-scrollbars-vertical} #confirmingAppraisal { position: relative; left: 0; top: 0; margin: 0 auto; padding: 0; width: 800px; min-height: 800px; overflow: auto; height: 1000px; background-color: #e9e9e9; } .dashed-box {position: absolute;z-index: 9997; border: 1px dashed red; width: 0px; height: 0px;left: 0px; top: 0px; overflow: hidden;} #moving_box {background-color: #0A90DB;} .question-box { position: absolute; z-index: 9998; /*background: red;*/ /* older safari/Chrome browsers */ -webkit-opacity: 0.8; /* Netscape and Older than Firefox 0.9 */ -moz-opacity: 0.8; /* Safari (pre WebKit!) Safari browser with old khtml kernel*/ -khtml-opacity: 0.8; /* IE9 + etc...modern browsers */ opacity: .8; /* IE 4-9 */ filter: alpha(opacity=80); /*This works in IE 8 & 9 too*/ -ms-filter: "progid:(Opacity=80)"; /*IE4-IE9*/ filter: progid:(Opacity=80); } .question-border {z-index: 9999;border: 2px dashed red;overflow: hidden; } .del-box { width: 17px; height: 17px; float: right; position: relative; margin-top: 1px; margin-right: 1px; z-index: 99999999999; background: url(); } </style> <script language="javascript"> = function() { function stopDefault(e) { if(e && ) (); else = false; return false; } function oBox() { var wId = "w"; var index = 0; var target = null; var startX = 0, startY = 0; var flag = false; var startL = 0, startT = 0, boxHeight = 0, boxWidth = 0; var boxObj = ("confirmingAppraisal"); var frame = $("#confirmingAppraisal"); var referenceSize = { "pos": (), "width": (), "height": () } var newMarkPos = { "left": startL,/////The distance to the left of the mouse when pressed "top": startT/////The distance above the mouse distance when pressed } //Mouse click = function(e) { flag = true; var e = || e; target = || ; //Get a reference to the document object //,compatible if() { stopDefault(e) } var scrollTop = ; var scrollLeft = ; var ePageX = + scrollLeft; var ePageY = + scrollTop; //The distance from the page when pressed startX = ePageX; startY = ePageY; //The distance between the left and upper mouse distance when pressed startL = startX - ; startT = startY - ; index++; // If the mouse is pressed on the box if((/del-box/i)) { // Allow drag flag = false; // Set the id of the current box to moving_box var movingBox = ("moving_box") if(movingBox !== null) { ("id"); } = "moving_box"; removeBox(target); } else { var div = ("div"); = wId + index; = "dashed-box"; (div); div = null; } } // Mouse leave = function(e) { var e = || e; if(boxWidth >= 1 || boxHeight >= 1) { (dragBox(wId + index)); index++; var div = ("div"); var spanObj = ("span"); = 'del-box'; (spanObj); = "question-box question-border"; = + "px"; = + "px"; = boxWidth + "px"; = boxHeight + "px"; (div); div = null; boxWidth = 0; boxHeight = 0; } else { if(flag) { (dragBox(wId + index)); } } flag = false; } //Mouse Move = function(e) { var e = || e; stopDefault(e) if(flag) { var scrollTop = ; var scrollLeft = ; var ePX = + scrollLeft; var ePY = + scrollTop; var disW = ePX - startX; var disH = ePY - startY; var L = startL + disW; var T = startT + disH; if(disW > 0) { if(L >= 0) { boxWidth = disW } = startL; } else { if(L <= 0) { L = 0; boxWidth = startL; } boxWidth = (startL - L); = L; } if(disH > 0) { if(T >= 0) { boxHeight = disH } = startT; } else { if(T <= 0) { T = 0; boxHeight = startT; } boxHeight = (startT - T) = T; } dragBox(wId + index). = + "px"; dragBox(wId + index). = + "px"; dragBox(wId + index). = (boxWidth) + "px"; dragBox(wId + index). = (boxHeight) + "px"; stopDefault(e) } } var dragBox = function(id) { return (id); } }; oBox(); function removeBox(obj) { if(obj) { var confirmingAppraisal = ('confirmingAppraisal'); if( == 'del-box') { var objId = (); (); } else { alert(123) } } }; } </script> </head> <body style="overflow: hidden"> <div class="confirming-appraisal"> <div > <img src="" style="position: absolute;width: 1000px;height: 1600px;"> </div> </div> </body> </html>
Interested friends can use the following tools to test the above code running effect:
Online HTML/CSS/JavaScript code running tools:
http://tools./code/HtmlJsRun
Online HTML/CSS/JavaScript front-end code debugging and running tools:
http://tools./code/WebCodeRun
For more information about jQuery, you can also view the topic of this site: "Summary of jQuery animation and special effects usage》、《Summary of jQuery switching effects and skills》、《Summary of jQuery extension skills》、《Summary of commonly used plug-ins and usages of jQuery》、《Summary of jQuery drag and drop special effects and skills》、《Summary of jQuery table operation skills》、《Summary of common classic effects of jQuery"and"Summary of jquery selector usage》
I hope this article will be helpful to everyone's jQuery programming.