Home > Article > Web Front-end > 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!