<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <script src="../public/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script> <title>Document</title> <style type="text/css"> #tanchu{ 300px; height: 300px; border-radius: 5px; background: rgba(0,0,0,0.3); position: absolute; display: none; } .close{ 10px; height:10px; float:right; margin-top: 10px; margin-right: 10px; } .close:hover{ cursor: pointer; } </style> </head> <body> <br /><br /><br /><br /><br /><br /><br /> <br /><br /><br /><br /><br /><br /><br /> <button>弹出</button> <div id="tanchu"> <div class="close">X</div> </div> <br /><br /><br /><br /><br /><br /><br /> <br /><br /><br /><br /><br /><br /><br /> <br /><br /><br /><br /><br /><br /><br /> </body> </html> <script type="text/javascript"> $(document).ready(function(){ var screenwidth,screenheight,tclleft,tctop,scrolltop; loadwz(); $("button").click(function(){ $("#tanchu").show(); }) $(".close").click(function(){ $("#tanchu").hide(); }) $(window).resize(function(){//对当前窗口大小进行计数: loadwz(); }) $(document).scroll(function(){//对元素滚动的次数进行计数: loadwz(); }) }) function loadwz(){ screenwidth = $(window).width(); screenheight = $(window).height(); scrolltop = $(document).scrollTop();//。 tcleft = (screenwidth-300)/2; tctop = (screenheight-300)/2; $("#tanchu").css({"left":tcleft,"top":tctop+scrolltop}); } </script>
效果图