设置 overflow:auto 的元素,会被内部的越界 absolute 元素撑出滚动条

没什么好详细说的,直接上结论,后面放代码和演示吧

结论

元素A设置了宽高和 overflow:auto ,内部有 absolute 的元素 b。若b超出了A的宽高,那么A会出现滚动条(内部指后代元素,不仅仅指子级)

解决方法没找到,只能先避免这种结构了。(参考 element-ui 的 dropdown 组件,发现它的下拉菜单都是加到 body 下的,可能也是为了避免这个问题?)


演示

<template>
  <section class="father">
    <section v-for="i in 3" :key="i" class="son">
      <span class="target"></span>
    </section>
  </section>
</template>
<style lang="scss" scoped>
.father {
  margin: 0 auto;
  width: 250px;
  background: pink;
  padding: 20px;
  height: 220px;
  overflow: auto;
  .son {
    position: relative;
    height: 60px;
    line-height: 60px;
    margin-bottom: 15px;
    background: skyblue;
    .target {
      display: none;
      position: absolute;
      z-index: 1;
      top: 10px;
      left: 150px;
      width: 150px;
      height: 300px;
      background: red;
    }
  }
  .son:nth-last-child(1) {
    margin-bottom: 0;
  }
  .son:hover .target {
    display: block;
  }
}
</style>
  • 粉色 father(设置了 overflow:auto 和高度)
    • 多个蓝色 son(设置 relative)
      • 每个蓝色 son 有自己的红色target(设置 absolute)

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值