在网站建设中有时候需要实现“点击按钮弹出对话框”的情形,那么如何实现呢?
思路:使用简单的javascript控制css。
演示:https://bujijam.github.io/demos/1/popup
知识点
javascript:
classList.toggle
方法:在元素中添加/消除类名。
setTimeout("代码/函数", 毫秒数)
方法:在指定的毫秒数后调用函数或计算表达式。
html:
css:
.class1.class2
:类选择器,同时拥有.class1
和.class2
的元素。
- flexbox(弹性盒子):
align-items
、justify-content
等。
代码
popup.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>css弹窗演示 | bujijam.ga</title> <link id="style-link" rel="stylesheet" href="./style.css" type="text/css"> </head> <body> <button id="clickme">点我!</button> <div class="popBackground"> <div class="modal"> <header> <h1>谢谢你点我o.<</h1><div id="close">×</div> </header> </br> <i style="opacity: 0.5;">每一个现在,都是以后的记忆。</i> <p>hi~</p> <p>这是一个ccs弹窗演示。</p> <p>作者:<a target="_blank" href="https://bujijam.ga/" title="我的主页">bujijam</a>,原文:<a target="_blank" href="https://blog.bujijam.ga/" title="我的博客">https://blog.bujijam.ga/</a></p> </div> </div> <script> const settingBox = document.querySelector('.modal'), openBtn = document.querySelector('#clickme'), closeBtn = document.querySelector('#close'), popBackgroundArea = document.querySelector('.popBackground');
openBtn.onclick = () =>{ popBackgroundArea.style.opacity = 1 popBackgroundArea.classList.toggle('showing') } closeBtn.onclick = () =>{ popBackgroundArea.style.opacity = 0 setTimeout("popBackgroundArea.classList.toggle('showing')", 200) }
</script> </body> </html>
|
style.css:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| * { margin: 0; padding: 0; box-sizing: border-box; } html { height: 100%; } body { display: flex; justify-content: center; align-items: center; background-color: rgb(187, 223, 187); color: darkolivegreen; } #clickme { color: white; padding: 16px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; cursor: pointer; background-color: white; color: black; border: 2px solid #4CAF50; border-radius: 3px; } #clickme:hover { background-color: #4CAF50; color: white; }
.popBackground { visibility: hidden; position: absolute; top: 0; left: 0; right: 0; bottom: 0; height: 100%; width: 100%; will-change: opacity; z-index: 12; display: flex; justify-content: center; align-items: center; background-color: rgba(0, 0, 0, 0.434); transition: opacity 0.2s 0s ease-in-out; opacity: 0; } .popBackground.showing { visibility: visible; opacity: 1; } .modal{ width: 40em; height: auto; background-color: rgb(240, 255, 241); margin: 1em; border-radius: 1em 1em 1em 1em; padding: 1em; box-shadow: 0px 10px 15px rgba(0,0,0,0.1); } header { display: flex; align-items: center; justify-content: space-between; } #close { font-size: 30px; cursor: pointer } #close:hover{ color: darkseagreen; } a { color: #4CAF50; text-decoration: none; cursor: pointer; }
|
下载
点此下载源代码