2017-08-17 95 views
0

我有固定元素和滚动条的问题。有没有办法将父元素的滚动条后面的固定元素移动?这里是fiddle父元素的固定元素叠加滚动条

body { 
 
    margin: 0; 
 
} 
 

 
.scrollable-container { 
 
    height: 100vh; 
 
    overflow-y: scroll; 
 
} 
 

 
.very-long-content { 
 
    height: 5000px; 
 
} 
 

 
.fixed-element { 
 
    background-image: url(http://space-kids.org/wp-content/uploads/2016/02/jupiter.png); 
 
    background-size: 100% 100%; 
 
    height: 300px; 
 
    position: fixed; 
 
    right: -100px; 
 
    top: calc(50% - 150px); 
 
    width: 300px; 
 
}
<section class="scrollable-container"> 
 
    <section class="very-long-content"> 
 
     <div class="fixed-element"></div> 
 
    </section> 
 
</section>

+2

设置'的z-index:-1;' –

回答

0

您可以添加一个负z-index.fixed-element(或者,在一般情况下,要确保它比一个.scrollable-container,默认为0小)。请注意,这会使.scrollable-container的内容也显示在前面(包括.very-long-content的内容)。

我会推荐你​​,如果可能,不要嵌套固定元素,因为they are always positioned relative to the screen's viewport

0

图片,显示 Fixed-element

这是你的固定元素应该是怎么样的

.fixed-element { 
    background-image: url(http://space-kids.org/wp-content/uploads/2016/02/jupiter.png); 
    background-size: 100% 100%; 
    height: 300px; 
    position: fixed; 
    right: -100px; 
    top: calc(50% - 150px); 
    width: 300px; 
    z-index: -1000; 
}