首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >转换:转换和z索引

转换:转换和z索引
EN

Stack Overflow用户
提问于 2017-04-09 14:14:40
回答 1查看 1.6K关注 0票数 3

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

HTML

代码语言:javascript
复制
<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

代码语言: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;
}

JavaScript

代码语言: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');
});

科德芬

您可以看到,在.choice-content动画期间,.choice会干扰其他.choice。有什么办法可以避免吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-09 14:43:56

您已将z-index分配给每个.choice,这是正确的,但您忘了将position给他们。科德芬

代码语言:javascript
复制
.choice {
    transition: .6s;
    margin-bottom: -264px;
    z-index: 0;
    position: relative;
}

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43307735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档