前端文章收集

CSS fixed 布局嵌套的一些问题

2018-09-28  本文已影响0人  JacobMa1996
前言

之前遇到过fixed布局嵌套的一些场景,fixed布局是针对body定位的,但是在某些特殊情况下,子级的fixed会受到父级fixed的影响,除了需要z-index来区分层级,还要注意transform的使用(貌似好多地方transform都会引发问题)

fixed的使用

一般的场景是一个遮罩加一个弹框

<div class="modal">
    <div class="content">
        <div class="children"></div>
    </div>
</div>
.modal {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    height: 100%;
    width: 100%;
    z-index: 999;
    background: rgba(0, 0, 0, .5)
}

.modal .content {
    width: 300px;
    height: 200px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate3d(-50%, -50%, 0)
}

效果是这样的


image.png

这个时候在内部的children再使用fixed布局时,就会被父级的fixed元素限制住

.modal .content .children {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    background: #123456
}
image.png
解决方法

1、父级不使用tansform
2、不进行嵌套

上一篇 下一篇

猜你喜欢

热点阅读