简体   繁体   English

使用css将鼠标悬停在另外两个元素上时动画化元素

[英]Animating an element when hovering on two other element using css

i am trying to animate a div when hovering on another div. 当我在另一个div上盘旋时,我试图动画div。 Ihave following code 我有以下代码

html HTML

<div>
</div>
<div class="content">
</div>

css 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;
}

What i am trying to do is, i need to animate .content to margin-top: -100px and margin-left: 50px; 我想要做的是,我需要动画.content到margin-top:-100px和margin-left:50px; from its default position, when the user is hover on one of these elements. 当用户将鼠标悬停在其中一个元素上时,从其默认位置开始。 But atm with this code it only works when the user hover on the .content and it animate the other way. 但是使用此代码的atm仅在用户将鼠标悬停在.content上并且以其他方式设置动画时才有效。 (from margin-top: -100px and margin-left: 50px; to default position). (从margin-top:-100px和margin-left:50px;到默认位置)。 Please excuse my english 请原谅我的英语

jsfiddle-> http://jsfiddle.net/fTAUk/1/ jsfiddle-> http://jsfiddle.net/fTAUk/1/

You need to wrap the content in the div and use a child selector. 您需要将内容包装在div中并使用子选择器。 Note I gave the larger div an id as well. 注意我也给了较大的div一个id。 Here is the css: 这是css:

JS Fiddle 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;
}

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

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