Implement pop-up box effect based on JavaScript_javascript skills
Pop-up boxes are an indispensable part of website pages. Today, with the help of the Script House platform, I will share with you how to use js to achieve a simple pop-up box effect. This article is not well written, so please forgive me!
First, let’s analyze the components of the pop-up box. A simple pop-up box is divided into head, content, and tail. The head has a title and a close button, the content can be graphics, media, iframe, flash, etc., and the tail is the button ( Confirm, cancel, etc.), I will not add buttons at the end of this example. This example mainly implements the core part of the pop-up box.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> body, div{ padding: 0; margin: 0; } html, body { width: 100%; height: 100%; } a { text-decoration: none; } .pop { border-radius: 5px; background-color: #fff; border: #eee 1px solid; position: absolute; width: 350px; left: 35%; top: 25%; } .pop-title { background-image: linear-gradient(#eee,#efefef); position: relative; cursor: pointer; } .pop-title h3,.pop-title a{ display: inline-block; } .pop-title h3{ font-size: 14px; margin: 0; padding: 5px; } .pop-title a { position: absolute; top: 5px; right: 5px; } .pop-content { padding: 10px; } </style> </head> <body> <div> <div> <h3>消息</h3> <a href="javascript:;">X</a> </div> <div> 弹出框已显示 </div> <div></div> </div> </body> </html>
A pop-up box will turn on the movement mode when the head is pressed, and prohibit movement when the head is released. In fact, this is what it means. Of course, the logic is relatively simple.
Here we may think of several variables, the X and Y coordinates of movement, the switch and prohibition of movement. Then we add onmousedown and onmouseup events to the title.
Onmousedown event mainly starts movement.
The logic in the onmouseup event is mainly to close the movement and disable the movement of the pop-up box.
Then we need to move, and when we need to move, we need to move within a certain range. Here we are moving inside the body. So add the onmousemove event to the body. What we do here is to move the position of the pop-up box.
These three events mainly combine the position attribute in CSS and the coordinates of the attributes in the Event event in JS.
var pop = document.getElementsByClassName("pop")[0]; var pop_title = pop.getElementsByClassName("pop-title")[0]; var bd = document.body; var x = 0; var y = 0; var ismove = false; // 是否开启移动 var downx = 30; var downy = 30; pop_title.onmousedown = function (e) { x = e.pageX; y = e.pageY; downx = e.offsetX; downy = e.offsetY; ismove = true; } bd.onmousemove = function (e) { if (ismove) { var cx = e.pageX - downx; var cy = e.pageY - downy; pop.style.left = cx + "px"; pop.style.top = cy + "px"; x = e.x; y = e.y; } e.preventDefault(); } pop_title.onmouseup = function (e) { x = e.pageX; y = e.pageY; ismove = false; console.log("移动完成") }
After moving the pop-up box, we add a close event to the close button.
//关闭 var pop_close = pop.getElementsByClassName("pop-close")[0]; pop_close.onclick = function () { pop.parentNode.removeChild(pop); }
Okay, a simple pop-up box is implemented. The same code can be optimized and encapsulated by itself, and other functions can be added. Compatibility issues may need to be dealt with by yourself (versions before IE9).
That’s all for introducing the pop-up box effect using js. I hope it will be helpful to you!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
