• css h5 端弹窗时禁止底部页面滚动


    h5 端页面在弹窗时禁止底部页面滚动,在实现时,我尝试过几种方法。

    方法一: @touchmove.stop.prevent

    在遮罩层中添加 @touchmove.stop.prevent 可以实现禁止页面滚动,如下:

    <div class="dialog-mask" v-if="isDlgShow" @click="closeHandle(2)" @touchmove.stop.prevent>
    div>
    
    • 1
    • 2

    缺点

    但是这种方法有个问题:弹窗里面内容有滚动条的也会无法滚动。

    方法二:prevent touchmove

    通过prevent touchmove 阻止触摸滑动事件touchmove的默认行为,如下:

    // 弹窗的事件
    openHandle () {
    	  // 在弹窗打开时直接阻止目标元素的滑动事件的默认行为
          document.body.addEventListener('touchmove', this.scrollSetup, { passive: false })
          // 打开弹窗
          this.hideOrShowDlg()     
    },
    closeHandle (type) {
          document.body.removeEventListener('touchmove', this.scrollSetup)
          // 关闭弹窗
          this.hideOrShowDlg()
    },
    scrollSetup (e) {
          e.preventDefault()
          e.stopPropagation()
    },
    hideOrShowDlg(){
          this.showDlg = !this.showDlg
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    addEventListener

    addEventListener 的第三个参数是 {passive: false}

    语法格式:passive: Boolean

    表示 listener 永远不会调用preventDefault();如果 listener 仍然调用了这个函数,客户端将会忽略它并抛出一个控制台警告。

    缺点

    这种方法同样也有个问题:弹窗里面内容有滚动条的也会无法滚动。

    方法三:position:fixed

    通过 position:fixed,在弹窗打开时,将目标元素进行固定,在关闭时恢复。

    由于定位会改变元素在页面上的位置,所以需要在fixed前记录元素的位置,取消fixed之后将元素又滚动到原来的位置。

    代码如下所示:

    <script>
    export default {
      name: "",
      data () {
        return {
          showDlg: false
        }
      },
      watch: {
      },
      created () {
      },
      mounted () {   
      },
      methods: { 
        openHandle () {
          /** ------------------ 跳出弹窗页面禁止滚动设置开始 ------------------ */    
          this.preventScoll(true)
          /** ------------------ 跳出弹窗页面禁止滚动设置结束 ------------------ */
    
          // 打开弹窗
          this.hideOrShowDlg()      
        },
        closeHandle () {
          /** ------------------ 关闭弹窗时移除禁止页面滚动设置开始 ------------------ */ 
          this.preventScoll(false)
          /** ------------------ 关闭弹窗时移除禁止页面滚动设置结束 ------------------ */
    
          // 关闭弹窗
          this.hideOrShowDlg()
        },
        hideOrShowDlg(){
          this.showDlg = !this.showDlg
        },
        /**
          * 阻止背景滚动
          * @param  Boolean  flag    [是否阻止背景滚动]
        */
        preventScoll (flag) {
          if (flag) {
            const top = document.documentElement.scrollTop || document.body.scrollTop;
            document.body.style.cssText += `
                position: fixed;
                width: 100vw;
                left: 0;
                top: ${-top}px;
            `
          } else {
            const top = document.body.style.top;
            document.body.style.cssText += `
                position: static;
            `;
            window.scrollTo(0, Math.abs(parseFloat(top)))
          }
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    这种方法,实现了需求,目前还没发现不足之处。

  • 相关阅读:
    【grafana | clickhouse】实现展示多折线图
    Qt5.9.9交叉编译(带sqlite3、OpenSSL)
    金三银四面试必问:Redis真的是单线程吗?
    基于javaweb的ssm学校教务管理系统(管理员,教师,学生)
    【day03】流程控制语句
    App加速读取显示IPFS图片的解决方案和实现
    C++多态 万字详解
    第1讲:MyBatis简介与入门
    企业微信主体怎么转让给别人?
    计算机毕业设计ssm校园扶助综合服务平台的设计与实现r941j系统+程序+源码+lw+远程部署
  • 原文地址:https://blog.csdn.net/HH18700418030/article/details/125431930