摘要:第一種情況比較簡(jiǎn)單,彈框和頁(yè)面都不可滾動(dòng)第二種情況是彈框可滾動(dòng),頁(yè)面不可滾動(dòng)移動(dòng)端兼容性不好,可應(yīng)用于端適用于移動(dòng)端記錄開始滑動(dòng)的坐標(biāo),用于判斷滑動(dòng)方向未開始,已開始,滑動(dòng)中核心部分判定一次就夠
第一種情況比較簡(jiǎn)單,彈框和頁(yè)面都不可滾動(dòng)
this is a box
this is a box
this is a box
.mask{ position: fixed; top: 0; left: 0; z-index: 999; width: 100%; height: 100%; display: none; background: rgba(0,0,0,.7); } .box{ position: absolute; top: 50%; left: 50%; transform: translate3d(-50%, -50%, 0); padding: 10px; background: #fff; text-align: center; }
var oBtn = document.getElementById("btn"), oMask = document.getElementById("mask"), oBox = document.getElementById("box"), oClose = document.getElementById("close"); oBtn.onclick = () => { oMask.style.display = "block"; } oClose.onclick = () => { oMask.style.display = "none"; } oMask.addEventListener("touchmove", (e) => { e.preventDefault(); });
第二種情況是彈框可滾動(dòng),頁(yè)面不可滾動(dòng)
1.移動(dòng)端兼容性不好,可應(yīng)用于pc端
this is a box
this is a box
this is a box
this is a box
this is a box
this is a box
this is a box
.mask{ position: fixed; top: 0; left: 0; z-index: 999; width: 100%; height: 100%; display: none; background: rgba(0,0,0,.7); } .box{ position: absolute; top: 50%; left: 50%; transform: translate3d(-50%, -50%, 0); width: 200px; height: 200px; padding: 10px; overflow-y: scroll; -webkit-overflow-scrolling : touch; background: #fff; text-align: center; }
var oBtn = document.getElementById("btn"), oMask = document.getElementById("mask"), oBox = document.getElementById("box"), oClose = document.getElementById("close"); oBtn.onclick = () => { oMask.style.display = "block"; document.body.style.height = "100%"; document.body.style.overflow = "hidden"; document.documentElement.style.overflow="hidden" } oClose.onclick = () => { oMask.style.display = "none"; document.body.style.height = "auto"; document.body.style.overflow = "scroll"; document.documentElement.style.overflow="scroll" }
2.適用于移動(dòng)端
this is a box
this is a box
this is a box
this is a box
this is a box
this is a box
this is a box
this is a box
.maskWrap{ position: fixed; top: 0; left: 0; z-index: 999; width: 100%; height: 100%; display: none; } .mask{ position: absolute; top: 0; left: 0; bottom: 0; right: 0; z-index: 1; background-color: rgba(0, 0, 0, .7); } .box{ position: absolute; top: 50%; left: 50%; transform: translate3d(-50%, -50%, 0); z-index: 2; width: 200px; height: 200px; overflow-y: scroll; -webkit-overflow-scrolling : touch; padding: 10px; background: #fff; text-align: center; }
var oBtn = document.getElementById("btn"), oMaskWrap = document.getElementById("maskWrap"), oMask = document.getElementById("mask"), oBox = document.getElementById("box"), oClose = document.getElementById("close"); oBtn.onclick = () => { oMaskWrap.style.display = "block"; } oClose.onclick = () => { oMaskWrap.style.display = "none"; } oMask.addEventListener("touchstart", (e) => { e.preventDefault(); }); let startY = 0; // 記錄開始滑動(dòng)的坐標(biāo),用于判斷滑動(dòng)方向 let status = 0; // 0:未開始,1:已開始,2:滑動(dòng)中 // 核心部分 oBox.addEventListener("touchstart", e => { status = 1; startY = e.targetTouches[0].pageY; }, false); oBox.addEventListener("touchmove", e => { // 判定一次就夠了 if (status !== 1) return; status = 2; let t = e.target || e.srcElement; let py = e.targetTouches[0].pageY; let ch = t.clientHeight; // 內(nèi)容可視高度 let sh = t.scrollHeight; // 內(nèi)容滾動(dòng)高度 let st = t.scrollTop; // 當(dāng)前滾動(dòng)高度 // 已經(jīng)到頭部盡頭了還要向上滑動(dòng),阻止它 if (st === 0 && py > startY) { // console.log(st + "/" + py + "/" + startY); e.preventDefault(); } // 已經(jīng)到低部盡頭了還要向下滑動(dòng),阻止它 if ((st === sh - ch) && py < startY) { // console.log(st + "/" + (sh - ch) + "/" + py + "/" + startY); e.preventDefault(); } }, false); oBox.addEventListener("touchend", e => { status = 0; }, false);
3.適用于移動(dòng)端和pc端
var oBtn = document.getElementById("btn"), oMaskWrap = document.getElementById("maskWrap"), oMask = document.getElementById("mask"), oBox = document.getElementById("box"), oClose = document.getElementById("close"); var isFixed = 0; oBtn.onclick = () => { oMaskWrap.style.display = "block"; isFixed = 1; } oClose.onclick = () => { oMaskWrap.style.display = "none"; isFixed = 0; } var bodyEl = document.body; var top = 0; function stopBodyScroll (isFixed) { console.log(isFixed) if (isFixed) { top = window.scrollY; bodyEl.style.position = "fixed"; bodyEl.style.top = -top + "px"; } else { bodyEl.style.position = ""; bodyEl.style.top = ""; window.scrollTo(0, top); // 回到原先的top } } // window.onscroll = stopBodyScroll(isFixed); document.addEventListener("onscroll", function (e) { stopBodyScroll(isFixed); })
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/113757.html
摘要:當(dāng)彈框彈出時(shí)原頁(yè)面內(nèi)容不能滾動(dòng),即將樣式設(shè)為原頁(yè)面的內(nèi)容就不會(huì)動(dòng)了當(dāng)彈框關(guān)閉后再將樣式改為默認(rèn)的中的寫一個(gè)函數(shù),再在彈框的中調(diào)用函數(shù)。彈出彈框 效果展示 實(shí)現(xiàn)原理 html結(jié)構(gòu)比較簡(jiǎn)單,即: 遮罩層 彈框 先寫覆蓋顯示窗口的遮罩層div.box,因?yàn)橐谡麄€(gè)窗口顯示固定,所以position要設(shè)為fixed,background設(shè)為灰色半透明,由于要遮住整個(gè)顯示屏,...
摘要:第一種情況比較簡(jiǎn)單,彈框和頁(yè)面都不可滾動(dòng)第二種情況是彈框可滾動(dòng),頁(yè)面不可滾動(dòng)移動(dòng)端兼容性不好,可應(yīng)用于端適用于移動(dòng)端記錄開始滑動(dòng)的坐標(biāo),用于判斷滑動(dòng)方向未開始,已開始,滑動(dòng)中核心部分判定一次就夠 第一種情況比較簡(jiǎn)單,彈框和頁(yè)面都不可滾動(dòng) this is a box this is a box this is a box ...
摘要:移動(dòng)端下彈框禁止背景滑動(dòng)茴字寫法有很多種,找到最適合的才是好的。 移動(dòng)端下彈框禁止背景滑動(dòng) 茴字寫法有很多種,找到最適合的才是好的。 以下下方法在一屛之內(nèi)是可行的 body;html 設(shè)置overflow:hidden .overflow-hidden{ height: 100%; overflow: hidden; } // 彈出時(shí) $(html, body,.pa...
摘要:移動(dòng)端下彈框禁止背景滑動(dòng)茴字寫法有很多種,找到最適合的才是好的。 移動(dòng)端下彈框禁止背景滑動(dòng) 茴字寫法有很多種,找到最適合的才是好的。 以下下方法在一屛之內(nèi)是可行的 body;html 設(shè)置overflow:hidden .overflow-hidden{ height: 100%; overflow: hidden; } // 彈出時(shí) $(html, body,.pa...
閱讀 2856·2021-11-22 15:22
閱讀 19297·2021-09-22 15:00
閱讀 1445·2021-09-07 09:58
閱讀 1246·2019-08-30 13:01
閱讀 2451·2019-08-29 16:27
閱讀 2354·2019-08-26 13:25
閱讀 1627·2019-08-26 12:13
閱讀 947·2019-08-26 11:53