简体   繁体   中英

Transition: translate and z-index

It seems like CSS transition: translate() has a conflict with z-index and I can't create a fold effect for my 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

You can see that .choice-content interferes with other .choice s during the transition animation. Is there a way to avoid that?

You have assigned z-index to each .choice which is correct but you forgot to give position to them. Codepen

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM