首页 > 其他分享 >h5 端页面在弹窗时禁止底部页面滚动

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

时间:2022-11-17 09:47:38浏览次数:50  
标签:滚动 ------------------ h5 touchmove document 弹窗 页面

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

方法一: @touchmove.stop.prevent

在遮罩层中添加 @touchmove.stop.prevent ,代码如下:

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

 

方法二: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
}

 

方法三: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>

标签:滚动,------------------,h5,touchmove,document,弹窗,页面
From: https://www.cnblogs.com/cwt981105/p/16898358.html

相关文章

  • 问题记录-前端开发避坑(7)--vue 原生h5开发及基础组件开发
    问题1问题描述与分析执行vueRouter的编程式导航时,报错:vuerouterTypeError:onCompleteisnotafunction检查发现是参数格式不对。这里使用了对象语法,而我错把qu......
  • Nginx开启Gzip压缩大幅提高页面加载速度
    刚刚给博客加了一个500px相册插件,lightbox引入了很多js文件和css文件,页面一下子看起来非常臃肿,所以还是把Gzip打开了。环境:Debian61、Vim打开Nginx配置文件vim /usr/local......
  • 页面性能优化:preload预加载静态资源
    本文主要介绍preload的使用,以及与prefetch的区别。然后会聊聊浏览器的加载优先级。preload提供了一种声明式的命令,让浏览器提前加载指定资源(加载后并不执行),在需要执行的......
  • WeChat 页面配置
    1、修改app.json文件,新建home和personcenter页面{"pages":["pages/home/home","pages/personcenter/personcenter","pages/index/index","pages/......
  • WPF打开windows弹窗与关闭
    //--------Window弹窗与关闭<DataGridTemplateColumnHeader="操作"Width="*"><DataG......
  • 钉钉H5微应用本地开发调试记录
    准备工具:手机(ios,安卓)安卓手机可以用网易Mumu模拟器,下载一个移动版钉钉钉钉开放文档本地开发的话,可以去钉钉后台管理系统,将你的本地ip地址配置成应用的首页,比......
  • 原生小程序与时间弹窗
    1.原生小程序与时间弹窗 <van-popup show="{{showDatetimePicker}}"roundposition="bottom"><van-datetime-picker title="选择开始时间" type="date"......
  • 在jsp页面int和String类型的相互转换
    浅浅地来做一个对比吧!.java文件int转成string类型:Strings=String.valueOf(intm);String转成int类型:intm=Integer.parseInt(Strings);.jsp页面中进行int和String类......
  • Vue路由的使用(页面跳转)
    路由不是内部功能所以需要先安装:一定要安装到相关项目中哦cnpminstall--savevue-router 需创建的文件夹及文件:......
  • App和H5交互
    前置操作Android//前端需要载NativeMethod对象到window上//在index.html文件中强烈建议添加下面代码<script>window.NativeMethod=NativeMethod;</script>An......