简体   繁体   English

CSS3 Transition,悬停以触发子元素无法正常工作的动画

[英]CSS3 Transition, hover to trigger animation on child element not working

I would like my list item to trigger a css3 transition on its child item .pusher when hovered over. 我希望我的列表项在其子项目.pusher悬停时触发css3过渡。 I am used to doing this in JS and not css3 transitions, after reading some other SO questions I thought I understood how to do it, but it's not working correctly: 我习惯在JS而不是css3过渡中这样做,在阅读了一些其他的SO问题之后我认为我明白了怎么做,但它不能正常工作:

#sidebar ul {
    float: left;
    list-style: none;
    padding: 0;
    margin: 0;
    width: 100%;
}
#sidebar ul li {
    padding: 20px;
    position: relative;
    list-style: none;
    border-bottom: 1px solid #4a4a4a;
    cursor: pointer;
    -webkit-transition: max-width 0.5s ease;
    transition: max-width 0.5s ease;
}
#sidebar ul li a {
    color: #fff;
    z-index: 5;
    position: relative;
}
#sidebar ul li a:hover {
    text-decoration: none;
}
#sidebar ul li:hover > .pusher {
    max-width: 100px;
    height: 100%;
    background: #383838;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
}
#sidebar ul li:first-child {
    border-top: 1px solid #4a4a4a;
}

Pusher is actually being appended to li, with JS, but I don't think this should cause an issue? Pusher实际上是用JS附加到li,但我认为这不会引起问题? (edit: this does not appear to be the issue) (编辑:这似乎不是问题)

fiddle: http://jsfiddle.net/8KpQW/ 小提琴: http//jsfiddle.net/8KpQW/

You haven't defined a width for .pusher merely a max-width so it doesn't know how wide it is supposed to be. 您还没有为.pusher定义宽度,只是max-width因此它不知道应该有多宽。

Try this 试试这个

JSFiddle Demo JSFiddle演示

CSS extract CSS提取

.pusher {
    width:0;
    transition:width 0.5s ease;
}

#sidebar ul li:hover .pusher {
    width: 100px;
    height: 100%;
    background: #383838;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
}

The code below fixed the issue for me : 下面的代码为我解决了这个问题:

 #sidebar {
  height: 100%;
  margin-top: 74px;
  background: #303030;
  color: #fff;
}
#sidebar ul {
  float: left;
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
}
#sidebar ul li {
  padding: 20px;
  position: relative;
  list-style: none;
  border-bottom: 1px solid #4a4a4a;
  cursor: pointer;
}
#sidebar ul li a {
  color: #fff;
  z-index: 5;
  position: relative;
}
#sidebar ul li a:hover {
  text-decoration: none;
}
#sidebar ul li .pusher {
  height: 100%;
  width: 0px;
  background: #383838;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  -webkit-transition: width 0.2s ease-in-out;
  -moz-transition: width 0.2s ease-in-out;
  -o-transition: width 0.2s ease-in-out;
  transition: width 0.2s ease-in-out;
}
#sidebar ul li:hover > .pusher {
  width: 100%;
}

the transition property needs to be applied to the actual element to be animated, not the hover trigger element. 需要将transition属性应用于要设置动画的实际元素,而不是悬停触发器元素。 hover, merely needs to contain the property value that the animation will end on. 悬停,只需要包含动画将结束的属性值。

fiddle: http://jsfiddle.net/8KpQW/3/ 小提琴: http//jsfiddle.net/8KpQW/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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