使用JavaScript實(shí)現(xiàn)彈出層效果的簡單實(shí)例
聲明
閱讀本文需要有一定的HTML、CSS和JavaScript基礎(chǔ)
設(shè)計(jì)
實(shí)現(xiàn)彈出層效果的思路非常簡單:將待顯示的內(nèi)容先隱藏,在觸發(fā)某種條件后(如點(diǎn)擊按鈕),將原本隱藏的內(nèi)容顯示出來。
實(shí)現(xiàn)
<!DOCTYPE html>
<html>
<head>
<title>Window對(duì)象</title>
<meta charset="utf-8">
</head>
<body>
<a >百度一下</a>
<button type="button" id="open">打開彈出層</button>
<div style="display: none;background: lightblue;border:1px solid green;" id="toast"> <!-- 設(shè)置display屬性為none以隱藏內(nèi)容 -->
<p>這里是彈出層內(nèi)容</p>
<button type="button" id="close">關(guān)閉彈出層</button>
</div>
<script type="text/javascript">
var toast = document.getElementById("toast");
document.getElementById("open").onclick = function(e){ <!-- 定義點(diǎn)擊事件顯示隱藏內(nèi)容 -->
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){ <!-- 將顯示的內(nèi)容再次隱藏 -->
toast.style.display = "none";
}
</script>
</body>
</html>
顯示效果如下:

但是我們可以注意到,在彈出隱藏內(nèi)容之后我們還是可以通過鏈接進(jìn)入百度頁面。為了防止這種情況的發(fā)生,我們可以提供遮罩層將原先的頁面內(nèi)容全部遮住。代碼如下:
<!DOCTYPE html>
<html>
<head>
<title>Window對(duì)象</title>
<meta charset="utf-8">
</head>
<body>
<a >百度一下</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"> <!-- 設(shè)置display屬性為none以隱藏內(nèi)容 -->
<p>這里是彈出層內(nèi)容</p>
<button type="button" id="close">關(guān)閉彈出層</button>
</div>
</div>
<script type="text/javascript">
var toast = document.getElementById("toast");
var cover = document.getElementById("cover");
document.getElementById("open").onclick = function(e){ <!-- 定義點(diǎn)擊事件顯示隱藏內(nèi)容 -->
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){ <!-- 將顯示的內(nèi)容再次隱藏 -->
cover.style.display = "none";
}
</script>
</body>
</html>
這是再次測試下效果,如下圖:

總結(jié)
上述內(nèi)容只是簡單實(shí)現(xiàn)了彈出層效果,但是通過添加更多的代碼也可以在此基礎(chǔ)上實(shí)現(xiàn)更復(fù)雜的功能。
以上這篇使用JavaScript實(shí)現(xiàn)彈出層效果的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)div彈出層的方法
- 使用js實(shí)現(xiàn)關(guān)閉js彈出層的窗口
- js 點(diǎn)擊頁面其他地方關(guān)閉彈出層(示例代碼)
- js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼
- JS+CSS實(shí)現(xiàn)鼠標(biāo)經(jīng)過彈出一個(gè)DIV框完整實(shí)例(帶緩沖動(dòng)畫漸變效果)
- js實(shí)現(xiàn)隨屏幕滾動(dòng)的帶緩沖效果的右下角廣告代碼
- js實(shí)現(xiàn)緩沖運(yùn)動(dòng)效果的方法
- JS實(shí)現(xiàn)多物體緩沖運(yùn)動(dòng)實(shí)例代碼
- JS下拉緩沖菜單示例代碼
- JS實(shí)現(xiàn)側(cè)邊欄鼠標(biāo)經(jīng)過彈出框+緩沖效果
相關(guān)文章
微信小程序?qū)崿F(xiàn)頁面導(dǎo)航與傳參功能詳解
這篇文章主要為大家詳細(xì)介紹一下微信小程序?qū)崿F(xiàn)頁面導(dǎo)航的幾種方法以及幫助大家掌握微信小程序如何進(jìn)行傳遞參數(shù),感興趣的朋友可以了解一下2022-08-08
基于Bootstrap下拉框插件bootstrap-select使用方法詳解
這篇文章主要為大家詳細(xì)介紹了基于Bootstrap下拉框插件bootstrap-select的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
基于JavaScript實(shí)現(xiàn)數(shù)值型坐標(biāo)軸刻度計(jì)算算法(echarts的y軸刻度計(jì)算)
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)數(shù)值型坐標(biāo)軸刻度計(jì)算算法(echarts的y軸刻度計(jì)算),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,感興趣的朋友可以參考與一下2022-06-06
基于layer.js實(shí)現(xiàn)收貨地址彈框選擇然后返回相應(yīng)的地址信息
這篇文章主要介紹了基于layer.js實(shí)現(xiàn)收貨地址彈框選擇然后返回相應(yīng)的地址信息,需要的朋友可以參考下2017-05-05
JavaScript如何使用Promise實(shí)現(xiàn)分批處理接口請(qǐng)求
當(dāng)我們在開發(fā)時(shí)遇到需要同時(shí)發(fā)起百條接口請(qǐng)求怎么辦呢,本文主要來和大家介紹一下JavaScript如何使用Promise實(shí)現(xiàn)分批處理接口請(qǐng)求,需要的可以參考下2024-03-03

