为什么css动画不适用于外部元素?

时间:2015-07-16 09:40:03

标签: javascript jquery html css animation

以下代码正常运行



.wrapper {
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px; 
    border: 1px solid black;
}

#slide {
    position: absolute;
    left: -100px;
    width: 100px;
    height: 100px;
    background: blue;
    transition: 1s;
}

.wrapper:hover #slide {
    transition: 1s;
    left: 0;
}

<div class="wrapper">
    <img id="slide" src="http://lorempixel.com/output/cats-q-c-100-100-4.jpg" />
</div>
&#13;
&#13;
&#13;

但如果我将img标记放在包装器之外,则代码不起作用 那么在那种情况下我该怎么办? 但如果我将img标记放在包装器之外,那么代码就不起作用了 那么在那种情况下我该怎么办? 但如果我将img标记放在包装器之外,那么代码就不起作用了 那么在那种情况下我该怎么办?

&#13;
&#13;
.wrapper {
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px; 
    border: 1px solid black;
}

#slide {
    position: absolute;
    left: -100px;
    width: 100px;
    height: 100px;
    background: blue;
    transition: 1s;
}

.wrapper:hover #slide {
    transition: 1s;
    left: 0;
}
&#13;
<html>
<div class="wrapper">
    </div>

<img id="slide" src="http://lorempixel.com/output/cats-q-c-100-100-4.jpg" />
</html>
&#13;
&#13;
&#13;

提前致谢:)

1 个答案:

答案 0 :(得分:2)

您可以使用同级选择器.wrapper:hover + #slide

.wrapper {
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px; 
    border: 1px solid black;
}

#slide {
    position: absolute;
    left: -100px;
    width: 100px;
    height: 100px;
    background: blue;
    transition: 1s;
}

.wrapper:hover + #slide {
    transition: 1s;
    left: 0;
}

示例:

<html>
<div class="wrapper">
    </div>

<img id="slide" src="http://lorempixel.com/output/cats-q-c-100-100-4.jpg" />
</html>
{{1}}