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


    代码:

    <script>
    export default {
      name: "",
      data () {
        return {
          showDlg: false
        }
      },
      watch: {
      },
      created () {
      },
      mounted () {   
      },
      methods: { 
        openHandle () {
          /** ------------------ 跳出弹窗页面禁止滚动设置开始 ------------------ */
          // 出现弹窗时,为body元素添加position:fixed,这样主页面就禁止滑动,同时很好地解决了弹窗穿透的问题。
          // 获取原来的scrollTop 并将body的top修改为对应的值
          this.prevBodyStyle_scrollTop = document.body.scrollTop || document.documentElement.scrollTop
          this.prevBodyStyle_top = window.getComputedStyle(document.body, null).getPropertyValue('top')
          document.body.style.top = `-${this.prevBodyStyle_scrollTop}px`
          // 获取原来body的position 为了解决iOS上光标漂移的问题 将position修改为fixed
          this.prevBodyStyle_position = window.getComputedStyle(document.body, null).getPropertyValue('position')
          document.body.style.position = 'fixed'
          // 为了避免width空值的情况
          this.prevBodyStyle_width = window.getComputedStyle(document.body, null).getPropertyValue('width')
          document.body.style.width = '100%'
          /** ------------------ 跳出弹窗页面禁止滚动设置结束 ------------------ */
    
          // 打开弹窗
          this.hideOrShowDlg()      
        },
        closeHandle () {
          /** ------------------ 关闭弹窗时移除禁止页面滚动设置开始 ------------------ */
          document.body.style.top = this.prevBodyStyle_top || '0px'
          document.body.style.position = this.prevBodyStyle_position
          document.body.style.width = this.prevBodyStyle_width || '100%'
          document.body.scrollTop = document.documentElement.scrollTop = this.prevBodyStyle_scrollTop || 0
          /** ------------------ 关闭弹窗时移除禁止页面滚动设置结束 ------------------ */
    
          // 关闭弹窗
          this.hideOrShowDlg()
        },
        hideOrShowDlg(){
          this.showDlg = !this.showDlg
        }
      }
    }
    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

    页面效果:
    在这里插入图片描述

  • 相关阅读:
    Viola-Jones检测器(VJ)---学习笔记
    关于遍历,递归,Divide and Conque,回溯,Memorization和DP的一个小结
    好看的html网站维护源码
    qt实现了音乐播放器2.0版本
    Sqoop数据导出 第1关:Sqoop数据导出语法学习
    使用 Docker 部署 TaleBook 私人书籍管理系统
    Redis解决缓存问题
    分布式中间件(五):RocketMQ 源码
    Go语言的100个错误使用场景(55-60)|并发基础
    GIS数据获取:气象数据免费下载网站
  • 原文地址:https://blog.csdn.net/HH18700418030/article/details/125441743