解决滑动遮罩层,遮罩层下面内容滚动的问题

示例(vue业务场景,配合mint-ui)

<template>
  <div class="wrap" :class='show?"fix":""' >
       <mt-button type="primary" @click.native="handleClick">primary button</mt-button>
       <ul>
            <li v-for='(item,key) in list' :key='key'>{{item}}</li>
        </ul>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        show:false,
        list:[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
      };
    },
    methods: {
      handleClick() {
            // 获取当前的滚动高度
            let scrollTop = document.scrollingElement.scrollTop;
            this.$messagebox.alert('操作成功!').then(action => {
                if(action==='confirm'){
                    //使其盒子的删除fix类名
                    this.show=false;
                   //还原body的原来的滚动高度
                    document.scrollingElement.scrollTop = scrollTop;
                }
            });
            //使其盒子的添加fix类名
            this.show=true;
           //设置body的top值为当前滑动高度(固定遮罩层下的内容,禁止滑动)
            document.body.style.top = -scrollTop + 'px';
      }
    },
  };

</script>
<style lang="scss" scoped>
  .wrap {
    &.fix{
      position: fixed;
    }
  }

</style>

讲解

主要利用 position: fixed;,改变其position的值及其body的top值,来固定底层

  • 当点击按钮,遮罩层出现时,获取当前的滚动高度
  • 给要禁止滑动的底层(示例中的wrap)添加fix类名(即 position: fixed;)
  • 设置body的top值为当前滑动高度(固定遮罩层下的内容,禁止滑动)
  • 当遮罩层消失,删除fix类名,还原原来的滚动高度
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,842评论 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,859评论 1 45
  • 之前写的是用jq写的 过时了被我删除了 现在的这个方法比较完善 说的再多 不如复制运行看一下效果吧image....
    阳光之城alt阅读 6,340评论 0 2
  • _________________________________________________________...
    fastwe阅读 1,327评论 0 0
  • PanYanZhi阅读 157评论 0 0