博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
遮罩层 弹框 页面滚动
阅读量:6295 次
发布时间:2019-06-22

本文共 5046 字,大约阅读时间需要 16 分钟。

第一种情况比较简单,弹框和页面都不可滚动

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();});

第二种情况是弹框可滚动,页面不可滚动

1.移动端兼容性不好,可应用于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.适用于移动端

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; // 记录开始滑动的坐标,用于判断滑动方向let status = 0; // 0:未开始,1:已开始,2:滑动中// 核心部分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; // 内容可视高度    let sh = t.scrollHeight; // 内容滚动高度    let st = t.scrollTop; // 当前滚动高度    // 已经到头部尽头了还要向上滑动,阻止它    if (st === 0 &&  py > startY) {        // console.log(st + '/' + py + '/' + startY);        e.preventDefault();    }    // 已经到低部尽头了还要向下滑动,阻止它    if ((st === sh - ch) &&  py < startY) {        // console.log(st + '/' + (sh - ch) + '/' + py + '/' + startY);        e.preventDefault();    }}, false);oBox.addEventListener('touchend', e => {    status = 0;}, false);

3.适用于移动端和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);})

转载地址:http://qsvta.baihongyu.com/

你可能感兴趣的文章
怎么用Navicat Premium图标编辑器创建表
查看>>
Spring配置文件(2)配置方式
查看>>
MariaDB/Mysql 批量插入 批量更新
查看>>
ItelliJ IDEA开发工具使用—创建一个web项目
查看>>
solr-4.10.4部署到tomcat6
查看>>
切片键(Shard Keys)
查看>>
淘宝API-类目
查看>>
virtualbox 笔记
查看>>
Git 常用命令
查看>>
驰骋工作流引擎三种项目集成开发模式
查看>>
SUSE11修改主机名方法
查看>>
jdk6.0 + Tomcat6.0的简单jsp,Servlet,javabean的调试
查看>>
Android:apk签名
查看>>
2(2).选择排序_冒泡(双向循环链表)
查看>>
MySQL 索引 BST树、B树、B+树、B*树
查看>>
微信支付
查看>>
CodeBlocks中的OpenGL
查看>>
短址(short URL)
查看>>
第十三章 RememberMe——《跟我学Shiro》
查看>>
mysql 时间函数 时间戳转为日期
查看>>