To achieve an effect similar to Windows forms, drag in the middle to change the position of the div, drag on the edge to change the size of the div, and the mouse changes to the corresponding shape at the corresponding position as shown in the figure: (The screenshot does not show the mouse) The code is as follows: $(".test1").mousemove(function(e){ $(".test1").unbind("mousedown"); $(".test1").css("cursor","default"); //$("span > b").text(parseInt($("div").width())); var left = $(".test1").offset().left; var top = $(".test1").offset().top; // If the mouse is in the middle if(e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","move"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var y = e.pageY - $(".test1").offset().top; $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"left":e.pageX - x, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is in the upper left corner if(e.clientX - left < 10 && e.clientY - top < 10) { $(".test1").css("cursor","nw-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is on top if(e.clientY - top < 10 && e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","n-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is in the upper right corner if(e.clientY - top < 10 && e.clientX-left > parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","ne-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is on the right if(e.clientX-left > parseInt($(".test1").width()) - 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","e-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is in the lower right corner if(e.clientX-left > parseInt($(".test1").width()) - 10 && e.clientY-top > parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","se-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is down if(e.clientY-top > parseInt($(".test1").height()) - 10 && e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","s-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is at the bottom left if(e.clientY-top > parseInt($(".test1").height()) - 10 && e.clientX - left < 10) { $(".test1").css("cursor","sw-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //If the mouse is on the left if(e.clientX - left < 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","w-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); } }); This is the end of this article about how to use jQuery to change the position and size of a div by dragging it with the mouse. For more information about how to use jQuery to drag a div with the mouse, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of mysql exists and not exists examples
>>: How to configure two-way certificate verification on nginx proxy server
Table of contents 1. Write an HTML, the first Vue...
The installation of mysql-5.7.17 is introduced be...
grammar background: linear-gradient(direction,col...
This article example shares the specific code of ...
Table of contents 1. What is event delegation? 2....
Download foreign images using Alibaba Cloud Image...
How to allow remote connection in MySql To achiev...
1. Concept They are all attributes of Element, in...
This article shares the specific steps of replaci...
The Linux operating system has revolutionized the...
In the horizontal direction, you can set the cell...
1. Introduction By enabling the slow query log, M...
> Deploy MySQL 5.7 cluster master & slave ...
<br />The most common mistake made by many w...
1. Parent div defines pseudo-classes: after and z...