无法向菜单添加转换

问题描述 投票:2回答:2

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

$(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>
html css
2个回答
1
投票

当您使用它添加某种变换时,过渡会起作用。我尝试了以下,它就像一个魅力。

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

}

希望能给出更多的澄清。


1
投票

你实际上是在display<nav>之间切换noneblock。这只是使<nav>出现和消失。如果你想制作动画,那么你可以通过使用bootstrap类来创建一个实际的下拉列表,如下所示;

$(document).ready(function(){
    $('.menu-toggle').on('click',function(){
        $('.menu-toggle').toggleClass('active');
    });
});
<!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>

警告:您与html链接的CSS必须在引导CDN之后,否则您在.clearfox上应用的CSS将无效。

这就是我使用嵌入式(在html文档中)CSS的原因。

© www.soinside.com 2019 - 2024. All rights reserved.