首頁  >  文章  >  web前端  >  javascript怎麼實作彈出層

javascript怎麼實作彈出層

藏色散人
藏色散人原創
2023-02-10 10:21:441710瀏覽

javascript實作彈出層的方法:1、建立一個HTML範例檔案;2、將待顯示的內容先隱藏,在觸發點擊條件後,將原本隱藏的內容顯示出來,程式碼如「document. getElementById("open").onclick = function(e){...}」;3、提供遮罩層將原先的頁面內容全部遮罩即可。

javascript怎麼實作彈出層

本教學操作環境:Windows10系統、javascript1.8.5版本、Dell G3電腦。

javascript怎麼實作彈出層?

使用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>

 

顯示效果如下:

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 id="cover" style="display: none;position: fixed;width: 100%;height: 100%;top:0px;left:0px;background: gray;">       <!-- 通过遮罩层遮住背景 -->
    <div style="background: lightblue;border:1px solid green;" id="toast">         <!-- 设置display属性为none以隐藏内容             -->
     <p>这里是弹出层内容</p>
      <button type="button" id="close">关闭弹出层</button>
  </div>
</div>
<script type="text/javascript">
    var toast = document.getElementById("toast");
    var cover = document.getElementById("cover");
    document.getElementById("open").onclick = function(e){           <!--    定义点击事件显示隐藏内容          -->
        cover.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){               <!--      将显示的内容再次隐藏         -->
        cover.style.display = "none";
    }
</script>
</body>
</html>

這是再次測試下效果,如下圖:

javascript怎麼實作彈出層

#總結

上述內容只是簡單實現了彈出層效果,但是透過添加更多的程式碼也可以在此基礎上實現更複雜的功能。

推薦學習:《JavaScript影片教學

以上是javascript怎麼實作彈出層的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn