我试图动画一个div时,悬停在另一个div。我有以下代码
html<div>
</div>
<div class="content">
</div>
css div {
height: 100px;
width: 100px;
background: red;
transition: all 0.4s ease-in-out;
}
.content {
height: 20px;
width: 20px;
background: green;
display: none;
}
div:hover + .content{
display: block;
margin-top: -100px;
margin-left: 50px;
}
我想做的是,我需要动画。content为margin-top: -100px和margin-left: 50px;当用户悬停在其中一个元素上时,从其默认位置开始。但是,这段代码只在用户悬停在。content上时才会起作用,它会以另一种方式动画。(from margin-top: -100px and margin-left: 50px;到默认位置)。请原谅我的英文
jsfiddle -> http://jsfiddle.net/fTAUk/1/
您需要在div中包装内容并使用子选择器。注意我也给了更大的div和id。下面是css:
JS小提琴
#box {
height: 100px;
width: 100px;
background: red;
position: absolute;
transition: all 0.4s ease-in-out;
}
#content {
width: 20px;
background: green;
height: 0;
transition: all 0.4s ease-in-out;
position: margin-top: -100px;
margin-left: 50px;
-webkit-transition: all .8s ease;
-moz-transition: all .8s ease;
-ms-transition: all .8s ease;
-o-transition: all .8s ease;
transition: all .8s ease;
}
#box:hover > #content {
height: 20px;
}