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

How to upgrade all Python libraries in Ubuntu 18.04 at once

What is pip pip is a Python package management to...

Share 10 of the latest web front-end frameworks (translation)

In the world of web development, frameworks are ve...

A colorful cat under Linux

Friends who have used the Linux system must have ...

What to do if the container started by docker run hangs and loses data

Scenario Description In a certain system, the fun...

MySQL time types and modes details

Table of contents 1. MySQL time type 2. Check the...

Why is your like statement not indexed?

Preface This article aims to explain the most bor...

Summary of practical skills commonly used in Vue projects

Table of contents Preface 1. Use $attrs and $list...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...

Use of Linux sed command

1. Function Introduction sed (Stream EDitor) is a...

18 killer JavaScript one-liners

Preface JavaScript continues to grow and prosper ...

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first f...

A brief discussion on CSS cascading mechanism

Why does CSS have a cascading mechanism? Because ...

Native JS to achieve blinds special effects

This article shares a blinds special effect imple...

How to implement Svelte's Defer Transition in Vue

I recently watched Rich Harris's <Rethinki...

Vue must learn knowledge points: the use of forEach()

Preface In front-end development, we often encoun...