CSS翻译以使在父元素后面的动画滑动



当您单击标题中的链接时,我正在尝试使用变换来对元素的滑动进行动画。我遇到的问题是,所显示的元素被嵌套了几个层次,这就是我认为引起问题的原因 - 我需要该元素从父元素后面滑下。在此上下文中,设置z索引似乎不起作用。我在下面创建了一个JS小提琴 - 感谢任何帮助!在此演示中,绿色父容器应坐在黄色隐藏元件的顶部。

jQuery('.test-link').on('click', function(e) {
  e.preventDefault();
  jQuery('.container').toggleClass('active');
});
.outer-container {
  background: green;
  width: 100%;
  position: relative;
  z-index: 5;
}
.container .hidden-content {
  transition: all 0.2s ease;
  transform: translateY(-100%);
  position: absolute;
  z-index: -1;
  z-index: 1;
  width: 100vw;
  left: 0;
  right: 0;
  background: yellow;
}
.test-link {
  margin-left: 100px;
  display: block;
  position: relative;
  z-index: 3;
  width: 200px;
  background: red;
}
.container.active .hidden-content {
  transform: translateY(0%);
}
 
.other-content {
  position: relative;
  z-index: 4;
  background: blue;
}
.test-content {
  position: relative;
  z-index: 5; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="outer-container">
  <div class="test-content">
    <div class="container">
      <a href="#" class="test-link">TEST LINK</a>
      <div class="hidden-content">
        <h1>My Hidden Content</h1>
        <h1>My Hidden Content</h1>
      </div>
    </div>
  </div>
<div class="other-content">
 <h2>This should be overlaid by the sliding out content</h2>
 </div>
</div>

您可以使用负z-index

做到这一点

jQuery('.test-link').on('click', function(e) {
  e.preventDefault();
  jQuery('.container').toggleClass('active');
});
.another-container {
  position: relative;
  z-index: 1;
}
.outer-container {
  background: green;
  width: 100%;
  position: relative;
}
.container .hidden-content {
  transition: all 0.2s ease;
  transform: translateY(-100%);
  position: absolute;
  background: yellow;
  z-index: -1;
}
.test-link {
  display: block;
  position: relative;
  width: 200px;
  background: red;
}
.container.active .hidden-content {
  transform: translateY(0%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="another-container">
<div class="outer-container">
  <div class="test-content">
    <div class="container">
      <a href="#" class="test-link">TEST LINK</a>
      <div class="hidden-content">
        <h1>My Hidden Content</h1>
        <h1>My Hidden Content</h1>
      </div>
    </div>
  </div>
</div>
</div>
<div>yellow box should be on top</div>

相关内容

  • 没有找到相关文章

最新更新