剪辑路径不适用于z-index

时间:2017-07-07 15:13:13

标签: css z-index clip-path

我无法将容器放在另一个使用剪切路径的容器的顶部。我尝试在任一容器上使用z-index,但它似乎没有帮助。

如果我从容器中删除了clipped-path类,那么块就会快乐地滑过容器。我收录了a fiddle of my problem.

这是我的代码:

window.onscroll = function(){
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  var para = Math.round(scrollTop / 1.2);
  document.querySelector('#block').style.transform = "translate3d(0px," + para + "px,0px)";
}
body {margin: 5px;}

#main {height:100vh;}
#below-main {height:100vh;}
#row1 {height:100vh;}


/* Paralellogram to slide underneath block
--------------------------------------------- */
#bluestripe {  
  height: 60vh;
  width:60vw;
  margin: 0 auto;
}

img {width: 100%;}

.clip-polygon {clip-path: polygon(100% 0, 100% 40%, 0 100%, 0 60%);}


/* Block to sit above parallelogram
--------------------------------------------- */
#block {
  height: 50px;
  width:100px;
  margin: 50px auto 0 auto;
  transform: translate3d(0px,0px,0px);
  background-color: #999;
}
<body>
  <div id="main">
    <div id="block">This needs to slide on top</div>
      <div id="bluestripe">
        <img id="sea" src="https://images.pexels.com/photos/6644/sea-water-ocean-waves.jpg?w=940&h=650&auto=compress&cs=tinysrgb" alt="" class="clip-polygon">
      </div>
    <div id="row1"></div>   
  </div>
</body>

1 个答案:

答案 0 :(得分:0)

要影响元素的z-index,必须将position设置为static以外的其他内容(默认值)。

对于#block,它没有position设置,因此它使用了源中元素顺序隐含的图层:它在剪切元素之前出现在源中,并且自然落在它下面。

要将其置于堆叠中较高的z-index,请为其指定positionz-index

#block {
    height: 50px;
    width: 100px;
    margin: 50px auto 0 auto;
    transform: translate3d(0px,95px,0px);
    background-color: #999;
    position: relative; /* Allows z-index to take affect */
    z-index: 2; /* Stacks it above the clipped layer, which has no position nor z-index and is at z-index 1 */
}