Home >Web Front-end >JS Tutorial >How to create a pop-up window effect with jQuery
This time I will show you how to create a pop-up window effect with jQuery. The pop-up window effect is often used in our projects. This article will give you a good analysis.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>弹窗</title> <script type="text/javascript" src="../jquery-3.2.1.min.js"></script> <style type="text/css"> *{margin: 0px;padding: 0px;} #login{height:300px;width: 300px;border: 1px solid #ddd;position: absolute; } #close{position: absolute;right: 0px;top: 0px;} </style> <script type="text/javascript"> // JS创建一个p标签,也就是节点元素 // window.onload=function(){ // document.createElement('p'); // } // 使用jQuery创建:$('<p>');带尖括号的是创建,不带是选择的意思 $(function(){ // var oDiv=$('<p>'); // $('body').append(oDiv); $('input').click(function(){ var oLogin=$('<p id="login"><p>用户名<input type="text"></p><p>密码<input type="text"></p><p id="close">X</p></p>');//此功能就相当于body中注释的代码 $('body').append(oLogin); oLogin.css('left',($(window).width()-oLogin.outerWidth())/2); oLogin.css('top',($(window).width()-oLogin.outerHeight())/2);//是弹窗能够出现在浏览器的中间 $("#close").click(function(){ oLogin.remove(); }) // jquery 中$()里window不用加引号 // 添加resize()浏览器窗口大小改变 // scroll():滚动条,以下的作用是当滚动条滚动时候,弹窗的位置也不变化 $(window).on("resize scroll",function(){ oLogin.css('left',($(window).width()-oLogin.outerWidth())/2+$(window).scrollLeft()); oLogin.css('top',($(window).width()-oLogin.outerHeight())/2+$(window).scrollTop()); }) }); }); </script> </head> <body> <input type="button" value="点击"> <!-- <p id="login"> <p>用户名<input type="text"></p> <p>密码<input type="text"></p> <p id="close">X</p> </p> --> </body> </html>
Points used:
jQuery creation:
$('<p>');
Position of the pop-up window:
oLogin.css('left',($(window).width()-oLogin.outerWidth())/2); oLogin.css('top',($(window).width()-oLogin.outerHeight())/2);
When the scroll bar scrolls, the position of the pop-up window changes:
$(window).on("resize scroll",function(){ oLogin.css('left',($(window).width()-oLogin.outerWidth())/2+$(window).scrollLeft()); oLogin.css('top',($(window).width()-oLogin.outerHeight())/2+$(window).scrollTop()); })
I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How ES6 uses the Set data structure to operate arrays
The above is the detailed content of How to create a pop-up window effect with jQuery. For more information, please follow other related articles on the PHP Chinese website!