菜单的动画有啥问题?

Posted

技术标签:

【中文标题】菜单的动画有啥问题?【英文标题】:What's wrong with the animation for the menu?菜单的动画有什么问题? 【发布时间】:2022-01-13 20:53:48 【问题描述】:

好吧,所以基本上我必须用 html 为学校做一个互联网站点,但只有 javascript 动画,所有代码都可以正常工作,但是当我想将我用于汉堡菜单的三行转换为带有 javascript 动画的交叉时什么都没有正在发生,我真的不知道为什么。 如果有人可以帮助我,我将一如既往地感激不尽,感谢您的阅读。

     <!doctype html>

    <head> 
    <meta charset="utf-8">
    <title>California Hotel</title>
    
    <style>
        * 
           margin: 0;
           padding: 0;
           

        
        body 
            font-family: Arial, Helvetica, sans-serif;
        


        .container
            width: 100%;
            height: 100vh;
            background-color: grey;
        
        .navbar
            width: 300px;
            height: 100%;
            background-color: blanchedalmond;
            position: fixed;
            top: 0;
            right: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            border-radius: 10% 0 0 5%;
        

        .hamburger-menu
            width: 35px;
            height: 30px;
            position: fixed;
            top: 50px;
            right: 50px;
            cursor: pointer;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
        
        .line
            width: 100%;
            height: 3px;
            background-color: black;
            
            
           

        

        .change .line-1
          transform: rotate(-45deg) translate(-8px,6px);

        
        .change .line-2
          opacity: 0;
        

        .change .line-3
          transform: rotate(45deg)  translate(-8px,-6px);;
        


        .nav-list
            text-align: right;
        

        .nav-item
            list-style: none;
            margin: 25px;

        

        .nav-links
            text-decoration: none;
            font-size: 20px;
            color: rgb(22, 73, 73);
            font-weight: 300;
            letter-spacing: 1px;
            text-transform: uppercase;
            position: relative;
            padding: 3px 0;

        
        .nav-links::before,
        .nav-links::after
            content: "";
            width: 100%;
            height: 2px;
            background-color: orange;
            position: absolute;
            left: 0;
            transform: scaleX(0);
            transition: transform 0.5s;
            
            

        
        
        .nav-links::after
            bottom: 0;
            transform-origin: right;
        

        .nav-links::before
            top: 0;
            transform-origin: left;
        

        .nav-links:hover::before,
        .nav-links:hover::after
            transform: scaleX(1);
        
    

      </style>
</head>
<body> 


    <div class="container">
        <nav class="navbar">
            <div class="hamburger-menu">
                <div class="line line-1"></div>
                <div class="line line-2"></div>
                <div class="line line-3"></div>
            </div>
            


            <ul class="nav-list">
                <li class="nav-item">
                    <a href="#" class="nav-links">A PROPOS</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-links">NOUS CONTACTER</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-links">NOS RESEAUX SOCIAUX</a>
                </li>
            </ul>
          </nav>
        </div>

           <script>
            const menuIcon  = document.querySelector(".hamburger-menu");
           const navbar = document.querySelector(".nabar");
            MenuIcon.addEventListener("click",() => 
          navbar.classList.toggle(".change"));
          </script>
    

         </body> 
      </html> 

【问题讨论】:

将 .nabar 更改为 .navbar 和 const MenuIcon 。注意 JavaScript 中的小写和大写字母 是的,我这样做了,但它不起作用:( 【参考方案1】:

这是您的正确代码: 失踪 ;并删除 .在 javascript 代码中上课之前。祝你好运。

               const MenuIcon = document.querySelector(".hamburger-menu");
               const navbar = document.querySelector(".navbar");
               MenuIcon.addEventListener("click", () => 
                   navbar.classList.toggle("change");
               );
      * 
               margin: 0;
               padding: 0;
           


           body 
               font-family: Arial, Helvetica, sans-serif;
           


           .container 
               width: 100%;
               height: 100vh;
               background-color: grey;
           

           .navbar 
               width: 300px;
               height: 100%;
               background-color: blanchedalmond;
               position: fixed;
               top: 0;
               right: 0;
               display: flex;
               justify-content: center;
               align-items: center;
               border-radius: 10% 0 0 5%;
           

           .hamburger-menu 
               width: 35px;
               height: 30px;
               position: fixed;
               top: 50px;
               right: 50px;
               cursor: pointer;
               display: flex;
               flex-direction: column;
               justify-content: space-around;
           

           .line 
               width: 100%;
               height: 3px;
               background-color: black;




           

           .change .line-1 
               transform: rotate(-45deg) translate(-8px, 6px);

           

           .change .line-2 
               opacity: 0;
           

           .change .line-3 
               transform: rotate(45deg) translate(-8px, -6px);
               ;
           


           .nav-list 
               text-align: right;
           

           .nav-item 
               list-style: none;
               margin: 25px;

           

           .nav-links 
               text-decoration: none;
               font-size: 20px;
               color: rgb(22, 73, 73);
               font-weight: 300;
               letter-spacing: 1px;
               text-transform: uppercase;
               position: relative;
               padding: 3px 0;

           

           .nav-links::before,
           .nav-links::after 
               content: "";
               width: 100%;
               height: 2px;
               background-color: orange;
               position: absolute;
               left: 0;
               transform: scaleX(0);
               transition: transform 0.5s;



           

           .nav-links::after 
               bottom: 0;
               transform-origin: right;
           

           .nav-links::before 
               top: 0;
               transform-origin: left;
           

           .nav-links:hover::before,
           .nav-links:hover::after 
               transform: scaleX(1);
           
       <div class="container">
           <nav class="navbar">
               <div class="hamburger-menu">
                   <div class="line line-1"></div>
                   <div class="line line-2"></div>
                   <div class="line line-3"></div>
               </div>



               <ul class="nav-list">
                   <li class="nav-item">
                       <a href="#" class="nav-links">A PROPOS</a>
                   </li>
                   <li class="nav-item">
                       <a href="#" class="nav-links">NOUS CONTACTER</a>
                   </li>
                   <li class="nav-item">
                       <a href="#" class="nav-links">NOS RESEAUX SOCIAUX</a>
                   </li>
               </ul>
           </nav>
       </div>

【讨论】:

ty 真的有时候我会犯这样的错误,我无法修复它们,因为我不知道是什么问题,所以我真的需要 ppls ty 先生的帮助

以上是关于菜单的动画有啥问题?的主要内容,如果未能解决你的问题,请参考以下文章

当第二次悬停在对象上时,有啥方法可以触发不同的动画?

动画 UITableView 的自动布局顶部约束导致崩溃,有啥线索吗?

在flash里面形状动画和补间动画有啥区别?

enter/exit 和 popEnter/popExit 动画有啥区别?

从inkscape设计到带有动画的html5有啥途径吗?

请问2D和3D有啥区别啊?