


JS implementation of mouse dragging and moving subforms_javascript skills
1. Subform
When designing a website, we need to design some modal subforms, such as
This step is easy to implement, you only need div css, please see the code:
opacity: 0.3;
position: fixed;
right: 0;
top: 0;
z -index: 1100;
}
.modal-window
{
background-color: #FFFFFF;
border: 1px solid #6B94AD; box-shadow: 5px 5px 5px #6B94AD;
font-family : 'Microsoft YaHei';
font-size: 12px;
height: 120px;
left: 50%;
margin-left: -160px;
margin-top: -160px;
opacity: 1;
position: fixed;
top: 50%;
width: 320px;
z-index: 1110;
}
.modal-window .head
{
height: 25px;
color: #fff;
background-image: -moz-linear-gradient (top, #4A8CC5, #2963A5); /* Firefox */
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #4A8CC5), color-stop(1 , #2963A5)); /* Saf4, Chrome */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#4A8CC5', endColorstr='#2963A5', GradientType='0'); /* IE */
}
.modal-window .head center
{
padding-top: 2px;
}
By adding the above html and css, you can easily achieve the effect of the above modal form. Among them, left: 50%; top: 50%; margin-left: -160px; margin-top: -160px; are to achieve the centering effect of this modal form.
Of course, the size of the modal form is fixed in the style class .modal-window. This does not mean that the size of the modal form cannot be modified. For example, I wrote the following code:
Copy code
.list-window
As shown in the picture
It can be seen that the implementation of this step is very simple. Mastering the css attributes of a few key lines can "completely abuse" this modal subform. You can draw inferences about other modal subforms.
2. How to drag and move the subform when the mouse clicks on the head of the subform? When jQ is introduced, we only need a few scripts to implement this small function. If you don’t believe it, let’s see
var left, top, $this;
$ (document).delegate('.modal-window .head', 'mousedown', function (e) {
left = e.clientX, top = e.clientY, $this = $(this).css(' cursor', 'move');
this.setCapture ? (
this.setCapture(),
this.onmousemove = function (ev) { mouseMove(ev || event); },
this.onmouseup = mouseUp
) : $(document).bind("mousemove", mouseMove).bind("mouseup", mouseUp);
});
function mouseMove(e) {
var target = $this.parents('.modal-window');
var l = Math.max((e.clientX - left Number(target.css('margin-left').replace(/px $/, '')) || 0), -target.position().left);
var t = Math.max((e.clientY - top Number(target.css('margin-top') .replace(/px$/, '')) || 0), -target.position().top);
l = Math.min(l, $(window).width() - target.width () - target.position().left);
t = Math.min(t, $(window).height() - target.height() - target.position().top);
left = e.clientX;
top = e.clientY;
target.css({ 'margin-left': l, 'margin-top': t });
}
function mouseUp (e) {
var el = $this.css('cursor', 'default').get(0);
el.releaseCapture ?
(
el.releaseCapture(),
el.onmousemove = el.onmouseup = null
): $(document).unbind("mousemove", mouseMove).unbind("mouseup", mouseUp);
}
This code is very short and can run smoothly in various browsers.
In fact, its implementation principle is very simple and can be roughly divided into three steps:
①When the mouse is clicked (mousedown) on the head of the modal form, immediately bind the mousemove and mouseup events to the document
② When the mouse does not bounce up (no mouseup), if the mouse moves within the form, the mouseMove function is activated, and the position of the entire form is moved in time by calculating the distance of the mouse movement.
③When the mouse bounces up (mouseup), call the mouseUp event to unbind the mousemove event and mouseup event bound to the document.
The principle of the whole process is: when the mouse is mousedown, the mouse movement event is transferred to the document, and the entire form is processed through the mouse movement event on the document.
In addition, there is a little trick in mouseMove, that is, the global left and top variables record the position of the last time the mouse stopped, and then compare the position of the mouse with the left and top variables the next time it moves to determine how much the mouse has moved. distance to move the entire modal subform accordingly.
After analyzing this piece of code, I found that it is quite easy to move the form or even any element on the document with the mouse
For example, if you want to change the size of the form by dragging and dropping, we only need to adjust the size of the form in the mouseMove event handler function. Do you find that you have learned another trick? A small step forward?
Some people may ask what setCapture and releaseCapture do respectively? In fact, this is for compatibility with IE. Only IE has these two functions. I despise IE here. setCapture allows the current element to capture all mouse events. If you don't use them, it may not be compatible with IE browser.

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
