悬停时反向动画

时间:2016-08-20 20:44:40

标签: css

我正在尝试创建一个下拉菜单,当我将鼠标悬停在菜单触发器上时,该菜单会向下滑动并再次向上滑动。 滑下工作完美,但我不能让滑动工作。如果我能用纯CSS获得解决方案,那就太好了 这是我的jsfiddle:https://jsfiddle.net/kp073okj/ 对于那些现在想要代码的人,这里是:

HTML-Code:

<div class="Dropdown">
<img src="Images/Dropdown.png" class="Dropdown-Button">
<div class="Dropdown-Content">
    <a href="#">Informationen</a>
    <a href="#">SocialMedia</a>
    <a href="#">Anmeldung</a>
    <a href="#">Dokumentation</a>
</div>
</div>

CSS-代码:

.Dropdown {
    position: fixed;
    z-index: 250;
    display: block;
    width: 2%;
    height: auto;
    margin-left: 80%;
}

.Dropdown-Button {
    position: relative;
    z-index: 280;
    font-size: 1.6vw;
    border: none;
    padding-bottom: 0.5em;
    width: 100%;
    height: auto;
    margin-top: 1em;
}

.Dropdown-Content {
    height: 0;
    overflow: hidden;
}

.Dropdown-Content a {
    color: white;
    text-decoration: none;
    padding: 0.5em 4.65em 0.5em 0.8em;
    display: block;
    text-align: left;
    font-size: 1.6vw;   
}


.Dropdown:hover .Dropdown-Content {
    display: block;
    height: 400%;
    animation-name: dropdown;
    animation-duration: 1s;
    position: absolute;
    z-index: 280;
    background-color: #303030;
    text-align: left;
    margin-left: -0.8em;
}

.Dropdown-Content a:hover {
    background-color: #555;
}


@keyframes dropdown {
    0% {height: 0%; transition: height 2s;}
    100% {height: 400%; transition: height 2s;}
}

1 个答案:

答案 0 :(得分:2)

更好的方法可能是简单地使用过渡来改变高度,而不是动画。毕竟,你只是在过渡动画。

简而言之......这里的动画有什么价值?

&#13;
&#13;
body { margin: 50px; background: #aaa;}

.Dropdown {
    position: fixed;
    z-index: 250;
    display: block;
    width: 2%;
    height: auto;
    /* margin-left: 80% commented out for snippet */;
}

.Dropdown-Button {
    position: relative;
    z-index: 280;
    font-size: 1.6vw;
    border: none;
    padding-bottom: 0.5em;
    width: 100%;
    height: auto;
    margin-top: 1em;
}

.Dropdown-Content {
    display: block;
    height: 0%;
    position: absolute;
    z-index: 280;
    background-color: #303030;
    text-align: left;
    margin-left: -0.8em;
    overflow: hidden;
    transition: height 2s;
}

.Dropdown-Content a {
    color: white;
    text-decoration: none;
    padding: 0.5em 4.65em 0.5em 0.8em;
    display: block;
    text-align: left;
    font-size: 1.6vw;   
}


.Dropdown:hover .Dropdown-Content {
    height: 400%;
    transition: height 2s;
}

.Dropdown-Content a:hover {
    background-color: #555;
}
&#13;
<div class="Dropdown">
<img src="http://placehold.it/140x140" class="Dropdown-Button">
<div class="Dropdown-Content">
    <a href="#">Informationen</a>
    <a href="#">SocialMedia</a>
    <a href="#">Anmeldung</a>
    <a href="#">Dokumentation</a>
</div>
</div>
&#13;
&#13;
&#13;