无法向菜单添加过渡

时间:2019-03-07 20:22:24

标签: html css

我无法在下拉菜单(移动设备)和酒吧图标上添加任何过渡效果。我应该在哪里放置过渡代码?另一方面,转换(如在悬停时成功更改字体颜色)会成功更改。我认为我的代码结构可能有问题,但看不到任何东西。

$(document).ready(function() {
    $('.menu-toggle').click(function() {
        $(this).toggleClass('active');
        $('nav').toggleClass('active');
    });
});
body {
    margin: 0;
    padding: 0;
    font-family: Lato, sans-serif;
    background-color: #333;
    height: 200vh;
}

a {
    text-decoration: none;
}

.clearfix {
    clear: both;
}

header {
    position: fixed;
    width: 100%;
    padding: 1vh 15vw;
    min-height: 100px;
    box-sizing: border-box;
    background-color: #fff;
}

.logo {
    color: #417475;
    text-transform: uppercase;
    font-weight: bold;
    font-size: 35px;
    height: 100px;
    line-height: 100px;
    float: left;
}

nav {
    float: right;
}

nav ul {
    display: flex;
}

nav ul li {
    list-style: none;
}

nav ul li a {
    display: block;
    margin: 10px 0;
    padding: 10px 20px;
}

nav ul li a:hover {
    color: #417475;
}

.menu-toggle {
    display: none;
    float: right;
    margin-top: 35px;
    font-size: 30px;
    cursor: pointer;
}

.menu-toggle:before {
    content: '\f0c9';
    font-family: fontAwesome;
    height: 30px;
}

.menu-toggle.active:before {
    content: '\f00d';
}

@media screen and (max-width: 768px) {
    .menu-toggle {
        display: block;
    }

    nav {
        display: none;
    }

    nav.active {
        display: block;
        width: 100%;
    }

    nav.active ul {
        display: block;
    }

    nav.active ul li a {
        margin: 0;
        text-align: center;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Portfolio 1</title>
    <link rel="stylesheet" href="style.css">
    <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
    <header>
        <a href="#" class="logo">Logo</a>
        <div class="menu-toggle"></div>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Offer</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
        <div class="clearfix"></div>
    </header>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="script.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

当您添加某种转换时,转换就起作用了。我尝试了以下方法,它就像一种魅力。

.menu-toggle.active {
  transition: 3s;
 transform: rotate(-180deg);

}

希望有更多的说明。

答案 1 :(得分:1)

您实际上是在display<nav>之间切换none的{​​{1}}。这只是使block出现和消失。如果要制作动画,那么 您可以按照以下步骤使用引导程序类来创建实际的下拉列表;

<nav>
$(document).ready(function(){
    $('.menu-toggle').on('click',function(){
        $('.menu-toggle').toggleClass('active');
    });
});

警告::与HTML链接的CSS必须在引导CDN之后出现,否则您在<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Portfolio 1</title> <link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <script src="adding_transition.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <style> body { margin: 0; padding: 0; font-family: Lato, sans-serif; background-color: #333; height: 200vh; /* transition: all 0.3s; */ } a { text-decoration: none; } .clearfix { clear: both; } header { position: fixed; width: 100%; padding: 1vh 15vw; min-height: 100px; box-sizing: border-box; background-color: #fff; } .logo { color: #417475; text-transform: uppercase; font-weight: bold; font-size: 35px; height: 100px; line-height: 100px; float: left; /* transition: all 0.3s ease; */ } nav { float: right; transition: all 0.3s; } nav ul { display: flex; } nav ul li { list-style: none; } nav ul li a { display: block; margin: 10px 0; padding: 10px 20px; } nav ul li a:hover { color: #417475; } .menu-toggle { display: none; float: right; margin-top: 35px; font-size: 30px; cursor: pointer; } .menu-toggle:before { content: '\f0c9'; font-family: fontAwesome; height: 30px; } .menu-toggle.active:before { content: '\f00d'; } .animate { transition: all 0.3s; } @media screen and (max-width: 768px) { .menu-toggle { display: block; } nav { display: block; width: 100%; } nav ul { display: block; } nav ul li a { margin: 0; text-align: center; } } </style> </head> <body> <header> <a href="#" class="logo">Logo</a> <a href="#list_drop" data-toggle="collapse" aria-expanded="false" ><div class="menu-toggle"></div></a> <nav> <ul id="list_drop" class="collapse list-unstyled"> <li><a href="#">Home</a></li> <li><a href="#">Offer</a></li> <li><a href="#">Portfolio</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <div class="clearfix"></div> </header> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script src="script.js"></script> </body> </html>上应用的CSS将无法工作。

这就是为什么我使用嵌入式(在html文档中)CSS。