vue项目中通过css完成遮罩层

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、html部分代码?

<div class="menu">
      <img
        v-if="!morePanelShow"
        class="more"
        @click="handleClickMore($event)"
        src="../assets/image/caidan.png"
      />
      <div class="more-panel" v-if="morePanelShow">
        <div class="more-panel-container">
          <img
            src="../assets/image/close.png"
            class="close"
            @click="handleClickClose"
          />
          <div v-for="(item, index) in fnList" :key="index" class="mp-item">
            {{ item.name }}
          </div>
        </div>
      </div>
    </div>

二、js部分代码

代码如下(示例):

export default {
  data() {
    return {
      fnList: [
        { name: '公告' },
        { name: '登录' },
        { name: '注册' },
        { name: '手册' },
        { name: '交流' },
        { name: '日志' }
      ],
      morePanelShow: false
    }
  },
  created() {},
  mounted() {
    document.addEventListener('click', this.bodyCloseMenus)
  },
  beforeDestroy() {
    document.removeEventListener('click', this.bodyCloseMenus)
  },
  methods: {
    bodyCloseMenus(e) {
      console.log(e)
      this.morePanelShow = false
    },
    handleClickMore(e) {
      e.stopPropagation()
      this.morePanelShow = true
    },
    handleClickClose(e) {
      e.stopPropagation()
      this.morePanelShow = false
    }
  }
}
</script>

三、css部分代码

代码如下(示例):

.container {
  display: flex;
  align-items: center;

  .menu {
    height: 100%;
    display: flex;
    align-items: center;

    > img {
      width: 24px;
      height: 24px;
      cursor: pointer;
    }
  }

  .more-panel {
    position: fixed;
    right: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    z-index: 2;
    width: 260px;
    background: rgba(0, 0, 0, 0.4);
    padding: 0 40px;
    padding-top: 100px;
    display: flex;
    flex-direction: column;

    .more-panel-container {
      position: relative;

      .close {
        width: 20px;
        height: 20px;
        position: absolute;
        top: -63px;
        right: 0px;
        cursor: pointer;
      }

      .mp-item {
        height: 48px;
        line-height: 48px;
        width: 100%;
        font-size: 18px;
        font-weight: 400;
        color: #ffffff;
        display: flex;
        justify-content: center;
        align-items: center;
        border-bottom: 1px solid rgba(255, 255, 255, 0.4);
        cursor: pointer;
      }
    }
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3的项目,可以通过以下步骤去掉遮罩层下方的滚动条: 1. 在遮罩层的样式,设置`position: fixed`和`overflow-y: scroll`,例如: ```css .mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.6); z-index: 999; overflow-y: scroll; } ``` 2. 在需要显示遮罩层的组件,使用Vue的`computed`属性计算出一个布尔值,用来判断是否需要显示遮罩层,例如: ```html <template> <div> <button @click="showMask = true">显示遮罩层</button> <div v-if="showMask" class="mask"></div> </div> </template> <script> export default { data() { return { showMask: false, }; }, computed: { bodyStyle() { return this.showMask ? { overflow: 'hidden' } : { overflow: 'auto' }; }, }, }; </script> <style> body { height: 100%; } </style> ``` 3. 在组件的`<body>`元素上绑定样式,根据是否显示遮罩层来决定是否隐藏滚动条,例如: ```html <template> <div> <button @click="showMask = true">显示遮罩层</button> <div v-if="showMask" class="mask"></div> </div> </template> <script> export default { data() { return { showMask: false, }; }, computed: { bodyStyle() { return this.showMask ? { overflow: 'hidden' } : { overflow: 'auto' }; }, }, mounted() { Object.assign(document.body.style, this.bodyStyle); }, updated() { Object.assign(document.body.style, this.bodyStyle); }, }; </script> <style> body { height: 100%; } </style> ``` 这样就可以在Vue3的项目去掉遮罩层下方的滚动条了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值