我无法在下拉菜单中获得过渡效果

时间:2019-11-20 10:40:54

标签: html css

我正在尝试为菜单的下拉菜单过渡效果,但是与其他情况不同,我无法使其正常工作。 我在这里以及Google上搜索了很多Theards。我没有找到解决办法。

这是我的代码:

.dropdown2 {
    position: relative;
    display: inline-block;
}

.dropdown-content2 {
    display: none;
    position: absolute;
    background-color: #e1e1e1;
    color: #000;
    min-width: 150px;
    height: 220px;
    z-index: 999;
    font-size: 20px;
    margin-right: -10px;
    top: 34px;
    transition: height 2s;
}

.dropdown-content2 a {
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    border-bottom: 5px solid white;
}

.dropdown2:hover .dropdown-content2 {
    display: block;
}


.dropdown2:hover .dropbtn2 {
    background-color: #e1e1e1;
}

HTML:

                <div class="menu_option dropdown2"><a href="?go=articles">articles</a>
                    <div class="dropdown-content2">
                        <a href="?go=news">news</a>
                        <a href="?go=reviews">reviews</a>
                        <a href="?go=guides">guides</a>
                        <a href="?go=art">art</a>
                    </div>
                </div>

它不起作用。

2 个答案:

答案 0 :(得分:0)

尝试这样..在不显示任何内容并且块过渡效果不起作用..所以现在我更改了不透明度

HTML

   <div class="menu_option dropdown2"><a href="?go=כתבות">כל הכתבות</a>
                    <div class="dropdown-content2">
                        <a href="?go=news">news</a>
                        <a href="?go=reviews">reviews</a>
                        <a href="?go=guides">guides</a>
                        <a href="?go=art">art</a>
                    </div>
                </div>

css

.dropdown2 {
    position: relative;
    display: inline-block;
}

.dropdown-content2 {
   opacity:0;
   visibility:hidden;
    position: absolute;
    background-color: #e1e1e1;
    color: #000;
    min-width: 150px;
    height: 220px;
    z-index: 999;
    font-size: 20px;
    margin-right: -10px;
    top: 34px;
    -webkit-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease
}

.dropdown-content2 a {
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    border-bottom: 5px solid white;
}

.dropdown2:hover .dropdown-content2 {
    opacity:1;
   visibility:visible;
    -webkit-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease
}


.dropdown2:hover .dropbtn2 {
    background-color: #e1e1e1;
}

答案 1 :(得分:0)

您不能从display: none过渡到display: block

幸运的是,在这种情况下,您不需要这样做。您只需从height: 0过渡到height: 220px

NB 您还 需要确保.dropdown-content2的样式为overflow: hidden,以确保父元素的内容超过高度时,它不会溢出边界并显示自己。

工作示例:

.dropdown2 {
    position: relative;
    display: inline-block;
}

.dropdown-content2 {
    position: absolute;
    background-color: #e1e1e1;
    color: #000;
    min-width: 150px;
    z-index: 999;
    font-size: 20px;
    margin-right: -10px;
    top: 34px;
    overflow: hidden;
}

.dropdown-content2 a {
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    border-bottom: 5px solid white;
}

.dropdown2 .dropdown-content2 {
    height: 0;
    transition: height 2s;
}

.dropdown2:hover .dropdown-content2 {
    height: 220px;
}


.dropdown2:hover .dropbtn2 {
    background-color: #e1e1e1;
}
<div class="menu_option dropdown2"><a href="?go=articles">articles</a>
<div class="dropdown-content2">
<a href="?go=news">news</a>
<a href="?go=reviews">reviews</a>
<a href="?go=guides">guides</a>
<a href="?go=art">art</a>
</div>
</div>