遮罩层使用 vw、vh 实现全屏覆盖。弹出框添加到遮罩层中并居中。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hangge.com</title> <script type="text/javascript" src="js/jquery.js"></script> <style> html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } .dialog-container { display: none; width: 100vw; height: 100vh; background-color: rgba(0,0,0,.35); text-align: center; position: fixed; top: 0; left: 0; z-index: 10; } .dialog-container:after { display: inline-block; content: ''; width: 0; height: 100%; vertical-align: middle; } .dialog-box { display: inline-block; border: 1px solid #ccc; text-align: left; vertical-align: middle; position: relative; } .dialog-title { line-height: 28px; padding-left: 5px; padding-right: 5px; border-bottom: 1px solid #ccc; background-color: #eee; font-size: 12px; text-align: left; } .dialog-close { position: absolute; top: 5px; right: 5px; font-size: 12px; } .dialog-body { background-color: #fff; } </style> </head> <body> <button onclick="$('#dialogContainer').show();">点击显示弹出框</button> <div id="dialogContainer" class="dialog-container"> <div class="dialog-box"> <div class="dialog-title">居中弹出框</div> <a onclick="$('#dialogContainer').hide();" class="dialog-close">关闭</a> <div class="dialog-body"> <img src="logo.png" class="demo-image" /> </div> </div> </div> </body> </html>
2,弹出框大小随视窗大小改变
(1)样例效果图
点击弹出按钮后,会显示一个在整个屏幕上居中显示的弹出框。
弹出框的大小不再由内容的大小决定,而是随视窗大小改变(宽高均为屏幕可视区域的 80%)。
点击关闭按钮后,则隐藏弹出框。
(2)样例代码
遮罩层使用 vw、vh 实现全屏覆盖。而弹出框的尺寸位置同样使用 vw、vh 设置。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hangge.com</title> <script type="text/javascript" src="js/jquery.js"></script> <style> html, body, div, span, button { margin: 0; padding: 0; border: 0; } button { width: 120px; height: 30px; color: #FFFFFF; font-family: "微软雅黑"; font-size: 14px; background: #28B995; } .dialog-container { display: none; width: 100vw; height: 100vh; background-color: rgba(0,0,0,.35); text-align: center; position: fixed; top: 0; left: 0; z-index: 10; } .dialog-box { top:10vh; left:10vw; width: 80vw; height: 80vh; text-align: left; position: absolute; border: 1px solid #ccc; display: flex; flex-direction: column; } .dialog-title { line-height: 28px; padding-left: 5px; padding-right: 5px; border-bottom: 1px solid #ccc; background-color: #eee; font-size: 12px; text-align: left; } .dialog-close { position: absolute; top: 5px; right: 5px; font-size: 12px; } .dialog-body { background-color: #fff; flex:1; overflow: auto; } </style> </head> <body> <button onclick="$('#dialogContainer').show();">点击显示弹出框</button> <div id="dialogContainer" class="dialog-container"> <div class="dialog-box"> <div class="dialog-title">居中弹出框</div> <a onclick="$('#dialogContainer').hide();" class="dialog-close">关闭</a> <div class="dialog-body"> <img src="logo.png" class="demo-image" /> </div> </div> </div> </body> </html>
五、显示大图时限制其最大尺寸
我们还可以通过视图单位来限制一些元素的最大宽度或高度,避尺寸过大而超出屏幕。
1,效果图