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
What is pip pip is a Python package management to...
In the world of web development, frameworks are ve...
Friends who have used the Linux system must have ...
Scenario Description In a certain system, the fun...
Table of contents 1. MySQL time type 2. Check the...
Preface This article aims to explain the most bor...
Table of contents Preface 1. Use $attrs and $list...
This article shares the specific code of JS+Canva...
1. Function Introduction sed (Stream EDitor) is a...
Preface JavaScript continues to grow and prosper ...
Before officially using Docker, let's first f...
Why does CSS have a cascading mechanism? Because ...
This article shares a blinds special effect imple...
I recently watched Rich Harris's <Rethinki...
Preface In front-end development, we often encoun...