jQuery implements the practice of changing the position and size of div by dragging the mouse

jQuery implements the practice of changing the position and size of div by dragging the mouse

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)

insert image description here

insert image description here

insert image description here

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:
  • Example of mouse dragging floating layer function implemented by jQuery [Drag div and any other tags]
  • jQuery div drag effect sample code
  • jQuery drag div, move div, pop-up layer implementation principle and examples
  • jQuery implements the pop-up Div layer window effect by clicking it (closable and draggable)
  • jquery drag to change div size
  • Use jQuery's easydrag plugin to implement a draggable DIV pop-up box
  • jQuery implements draggable DIV custom saving to data example
  • jQuery method to achieve Div drag + keyboard control comprehensive effect

<<:  Detailed explanation of mysql exists and not exists examples

>>:  How to configure two-way certificate verification on nginx proxy server

Recommend

Study notes to write the first program of Vue

Table of contents 1. Write an HTML, the first Vue...

Tutorial on logging into MySQL after installing Mysql 5.7.17

The installation of mysql-5.7.17 is introduced be...

CSS container background 10 color gradient Demo (linear-gradient())

grammar background: linear-gradient(direction,col...

JS+CSS to realize dynamic clock

This article example shares the specific code of ...

JavaScript event delegation principle

Table of contents 1. What is event delegation? 2....

How to allow remote connection in MySql

How to allow remote connection in MySql To achiev...

The difference between clientWidth, offsetWidth, scrollWidth in JavaScript

1. Concept They are all attributes of Element, in...

Detailed tutorial on replacing mysql8.0.17 in windows10

This article shares the specific steps of replaci...

Can Docker become the next "Linux"?

The Linux operating system has revolutionized the...

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

MYSQL slow query and log example explanation

1. Introduction By enabling the slow query log, M...

How to deploy MySQL 5.7 & 8.0 master-slave cluster using Docker

> Deploy MySQL 5.7 cluster master & slave ...

Some points on using standard HTML codes in web page creation

<br />The most common mistake made by many w...

A complete guide to clearing floats in CSS (summary)

1. Parent div defines pseudo-classes: after and z...