你的位置:首页 > 软件开发 > 网页设计 > 使用JavaScript实现弹出层效果

使用JavaScript实现弹出层效果

发布时间:2016-05-31 01:00:04
声明阅读本文需要有一定的HTML、CSS和JavaScript基础设计实现弹出层效果的思路非常简单:将待显示的内容先隐藏,在触发某种条件后(如点击按钮),将原本隐藏的内容显示出来。实现 <!DOCTYPE html><html><head> ...

使用JavaScript实现弹出层效果

声明

阅读本文需要有一定的HTML、CSS和JavaScript基础


设计

实现弹出层效果的思路非常简单:将待显示的内容先隐藏,在触发某种条件后(如点击按钮),将原本隐藏的内容显示出来。


实现

 

<!DOCTYPE html><html><head>  <title>Window对象</title>  <meta charset="utf-8"></head><body><a href="http://www.baidu.com">百度一下</a><button type="button" id="open">打开弹出层</button><div style="display: none;background: lightblue;border:1px solid green;" id="toast">     <!--   设置display属性为none以隐藏内容       -->  <p>这里是弹出层内容</p>  <button type="button" id="close">关闭弹出层</button></div><script type="text/javascript">  var toast = document.getElementById("toast");  document.getElementById("open").onclick = function(e){      <!--  定义点击事件显示隐藏内容     -->    toast.style.display = "block";    toast.style.position = "fixed";    var winWidth = window.innerWidth;    var winHeight = window.innerHeight;    var targetWidth = toast.offsetWidth;    var targetHeight = toast.offsetHeight;    toast.style.top = (winHeight - targetHeight) / 2 + "px";    toast.style.left = (winWidth - targetWidth) / 2 + "px";  }  document.getElementById("close").onclick = function(e){        <!--   将显示的内容再次隐藏     -->    toast.style.display = "none";  }</script></body></html>

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:使用JavaScript实现弹出层效果

关键词:JavaScript

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。