javascript - 转换 : translate and z-index

标签 javascript html css css-transitions

似乎 CSS transition: translate()z-index 有冲突,我无法为我的 block 创建折叠效果。

HTML

<div class="card">
  <div class="arrow-box">
    <span>Destinations</span>
  </div>
  <div class="choice" style="z-index: 1">
    <div class="choice-header">New York</div>
    <div class="choice-content" style="background: red"></div>
  </div>
  <div class="choice" style="z-index: 2">
    <div class="choice-header">Boston</div>
    <div class="choice-content" style="background: #801566"></div>
  </div>
  <div class="choice" style="z-index: 3">
    <div class="choice-header">Seattle</div>
    <div class="choice-content" style="background: green"></div>
  </div>
  <div class="choice" style="z-index: 4">
    <div class="choice-header">Washington</div>
    <div class="choice-content" style="background: #1e3180"></div>
  </div>
  <div class="choice" style="z-index: 5">
    <div class="choice-header">San Francisco</div>
    <div class="choice-content" style="background: #e5f400"></div>
  </div>
</div>

CSS

.choice {
  transition: .6s;
  margin-bottom: -264px;
  z-index: 0;
}

.choice-content {
  transition: .6s;
}

.choice-header {
  height: 44px;
  background: #49647a;
  transition: .6s;
  transition-timing-function: ease-in-out;
  text-align: center;
  line-height: 44px;
  color: white;
  font-size: 22px;
  font-family: 'PFBagueSansProLight', sans-serif;
  text-transform: uppercase;
  cursor: pointer;
}

.choice-header:after {
  content: '';
  position: absolute;
  margin-top: 15px;
  right: 22px;
  display: inline-block;
  width: 18px;
  height: 18px;
  background: url("/static/pages/img/polygon.png") no-repeat center;
  transition: 0.6s;
}

.choice:last-child {
  margin-bottom: 0;
}

.choice.selected {
  transform: translate3d(0, -265px, 0);
  margin-bottom: -265px;
}

.choice.selected > .choice-header:after {
  transform: rotate(180deg);
}

.choice-content {
  height: 265px;
}

.card {
  height: 527px;
  background: #ebebeb;
  overflow: hidden;
}

.arrow-box {
  position: relative;
  background: #49647a;
  height: 44px;
  margin-bottom: 260px;
}

.arrow-box:after {
  top: 100%;
  left: 50%;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  border-color: rgba(73, 100, 122, 0);
  border-top-color: #49647a;
  border-width: 16px 165px 0 165px;
  border-style: solid;
  margin-left: -165px;
}

.arrow-box span {
  text-align: center;
  vertical-align: middle;
  display: block;
  height: 44px;
  line-height: 44px;
  font-size: 22px;
  color: white;
}

JavaScript

$('.choice-header').click(function() {
  var elem = $(this).parents().eq(1).find('.choice');
  var i = $(this).hasClass('selected') ? 0 : 1;
  var n = elem.index($(this).parent());
  elem.removeClass('selected');
  elem.slice(0, n + i).addClass('selected');
});

Codepen

您可以看到 .choice-contenttransition 动画期间会干扰其他 .choice。有没有办法避免这种情况?

最佳答案

您已将 z-index 分配给每个正确的 .choice,但您忘记为它们提供 positionCodepen

.choice {
    transition: .6s;
    margin-bottom: -264px;
    z-index: 0;
    position: relative;
}

Point, Note:

Only works on positioned elements(position: absolute;, position: relative; or position: fixed;).

关于javascript - 转换 : translate and z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43307735/

相关文章:

html - 使用 css 正确模拟 "ribbon"边界的问题。我该如何纠正它?

html - 滚动禁用内容调整大小

JavaScript:正则表达式测试内外循环的不同结果

javascript - WoW Armory APi - 无法获得标题

Javascript 通过变量命名类

javascript - 删除部分img src

javascript - 如何从图片中提取图标并显示在页面中? (通过 javascript 或 jquery 或 css)

javascript - onclick 按钮在 Firefox 中不起作用

javascript - 如何将图像居中并调整大小为屏幕的 100% 宽度,但前提是 <1024 像素

jquery - 表格脱离 div