关于遮罩层+自定义弹出框的实现,透明度设置,子元素opacity不生效(不符合预期)问题的一点总结

最终效果:
在这里插入图片描述
这里是在vue中实现的,如果没有使用框架可以直接把title和提示内容换成想要的内容即可。
主框架:

<template>
  <div class="popupbox">
    <div class="main">
      <div class="title">{{title}}</div>
      <div class="content">
        <div class="info">{{alertInfo}}</div>
      </div>
      <div class="btn">
        <button>确定</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "提示",
    },
    alertInfo: {
      type: String,
      default: "提示内容",
    },
  },
};
</script>

想要实现遮罩层覆盖整个屏幕,设置最外层元素position为fixed比较合适。
为了让遮罩层出现半透明的状态,这里使用了opacity属性:
在这里插入图片描述
然后问题出现了:
在这里插入图片描述
很明显,我们希望中间的提示框是不透明的,但是这里很明显受到了父元素opacity的影响。
那如果把子元素的opacity设置为1,是不是就可以解决了?
在这里插入图片描述
很遗憾,结果没有任何改变。后面查阅资料才发现,子元素会继承父元素的opacity属性,如果子元素单独设置了opacity那么其真正的结果值是子元素与父元素opacity值的乘积。
所以这里opacity设置为1,其透明度仍然是0.5*1=0.5,(注意opacity的值为0-1,所以不能通过设置opacity为2来使透明度为1)
解决方案:
父元素设置背景使用rgba的形式来代替背景色+opacity:
在这里插入图片描述
rgba()前三位用来指定颜色,最后一位指定透明度。
最终代码:

<template>
  <div class="popupbox">
    <div class="main">
      <div class="title">{{title}}</div>
      <div class="content">
        <div class="info">{{alertInfo}}</div>
      </div>
      <div class="btn">
        <button>确定</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "提示",
    },
    alertInfo: {
      type: String,
      default: "提示内容",
    },
  },
};
</script>

<style scoped>
    .popupbox {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 100;
    /* 为防止页面中有其他定位元素的z-index较大,可以将这里的z-index设置的得大一些 */
    background-color: rgba(0,0,0,.5);
    /* opacity: 0.5; */
    
    }
    .popupbox .main {
    position: absolute;
    width: 300px;
    height: 200px;
    top: 100px;
    left: 50%;
    margin-left: -150px;
    background-color: white;
    border-radius: 10px;
    }
    .main .title {
    height: 35px;
    background-color: #2c3f7c;
    color: white;
    line-height: 35px;
    border-radius: 10px 10px 0 0;
    font-size: 14px;
    padding-left: 10px;
    }
    .main .content {
    width: 100%;
    height: 130px;
    display: flex;
    justify-content: center;
    align-items: center;
    }
    .main .btn{
        height: 35px;
        width: 100%;

    }
    .btn button{
        float: right;
        margin-right: 20px;
        background-color: #2c3f7c;
        outline: none;
        color: white;
        border: none;
        padding: 3px 8px;
    }
</style>

如果提示标题和内容确定可以直接把{{title}},{{alertinfo}}换成需要的内容即可,这样script标签可以去掉,template标签也可以去掉,在原生的html中使用

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用uniapp的组件和样式来自定义弹出层。以下是一种常见的实现方式: 1. 创建一个新的组件,例如"popup"组件。 2. 在该组件的模板中,可以使用`<slot></slot>`标签来放置弹出层的内容。 3. 在组件的样式中,设置弹出层的样式,例如位置、背景色、边等。 4. 在"popup"组件的脚本中,定义控制弹出层显示和隐藏的数据和方法。你可以使用`show`变量来控制弹出层的显示状态。 5. 在需要弹出层的页面中,引入"popup"组件,并根据需求设置弹出层的内容。 下面是一个简单的示例代码: 在"popup"组件的模板中(popup.vue): ```html <template> <div class="popup" :class="{ active: show }"> <div class="popup-content"> <slot></slot> </div> </div> </template> ``` 在"popup"组件的样式中(popup.vue): ```css <style scoped> .popup { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 9999; opacity: 0; pointer-events: none; transition: opacity 0.3s ease; } .popup.active { opacity: 1; pointer-events: auto; } .popup-content { background-color: #fff; padding: 20px; border-radius: 5px; } </style> ``` 在"popup"组件的脚本中(popup.vue): ```javascript <script> export default { data() { return { show: false }; }, methods: { togglePopup() { this.show = !this.show; } } }; </script> ``` 在需要弹出层的页面中,使用"popup"组件(index.vue): ```html <template> <div> <button @click="togglePopup">打开弹出层</button> <popup> <!-- 弹出层的内容 --> <h2>自定义弹出层</h2> <p>这是弹出层的内容。</p> </popup> </div> </template> <script> import Popup from '@/components/popup.vue'; export default { components: { Popup } }; </script> ``` 以上代码示例中,点击按钮将触发`togglePopup`方法,从而切换弹出层的显示和隐藏。你可以根据实际需求进行修改和扩展,比如添加动画效果、传递参数等。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值