在一些页面中,我们总会遇到一些弹窗不居中的时候,还要根据浏览器的大小来调整弹窗的弹出位置,之前我也遇到这样的问题,现在我把我知道的呈现给大家。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery怎么实现弹窗居中的测试</title> <style type="text/css"> .mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #000; opacity: 0.5; filter: alpha(opacity=50); display: none; z-index: 99; } .mess { position: absolute; display: none; width: 250px; height: 100px; border: 1px solid #ccc; background: #ececec; text-align: center; z-index: 101; } </style> <script type="text/javascript" src="../tu/jquery-1.11.3.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.btn').click(function() { $('.mask').css({'display': 'block'}); center($('.mess')); check($(this).parent(), $('.btn1'), $('.btn2')); }); // 居中 function center(obj) { var screenWidth = $(window).width(); screenHeight = $(window).height(); //当前浏览器窗口的 宽高 var scrolltop = $(document).scrollTop();//获取当前窗口距离页面顶部高度 var objLeft = (screenWidth - obj.width())/2 ; var objTop = (screenHeight - obj.height())/2 + scrolltop; obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'}); //浏览器窗口大小改变时 $(window).resize(function() { screenWidth = $(window).width(); screenHeight = $(window).height(); scrolltop = $(document).scrollTop(); objLeft = (screenWidth - obj.width())/2 ; objTop = (screenHeight - obj.height())/2 + scrolltop; obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'}); }); //浏览器有滚动条时的操作、 $(window).scroll(function() { screenWidth = $(window).width(); screenHeight = $(widow).height(); scrolltop = $(document).scrollTop(); objLeft = (screenWidth - obj.width())/2 ; objTop = (screenHeight - obj.height())/2 + scrolltop; obj.css({left: objLeft + 'px', top: objTop + 'px','display': 'block'}); }); } //确定取消的操作 function check(obj, obj1, obj2) { obj1.click(function() { obj.remove(); closed($('.mask'), $('.mess')); }); obj2.click(function() { closed($('.mask'), $('.mess')); }) ; } // 隐藏 的操作 function closed(obj1, obj2) { obj1.hide(); obj2.hide(); } }); </script> </head> <body> <input type="button" class="btn" value="弹窗效果按钮"/> <div>弹出确认框始终位于窗口的中间位置的测试</div> <div class="mask"></div> <div class="mess"> <p>确定要删除吗?</p> <p><input type="button" value="确定" class="btn1"/> <input type="button" value="取消"class="btn2"/></p> </div> </body> </html>
原文链接:https://www.qiquanji.com/post/7217.html
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
微信扫码关注
更新实时通知