转换:翻译和z索引

时间:2017-04-09 14:14:41

标签: javascript html css css3 css-transitions

似乎CSS Square: <span ng-bind="Square"></span> <br /> Cube: <span ng-bind="Cube"></span> transition: translate()存在冲突,我无法为我的块创建折叠效果。

HTML

z-index

CSS

<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>

的JavaScript

.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;
}

Codepen

您可以在$('.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'); }); 动画期间看到.choice-content干扰其他.choice。有没有办法避免这种情况?

1 个答案:

答案 0 :(得分:5)

您已为每个z-index分配了.choice这是正确的,但您忘了给positionCodepen

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

<强> Point, Note

     

仅适用于定位元素(position: absolute;position: relative;position: fixed;)。