現在Web制作を学んでいます。学習中、以下のjQueryで書かれたコードをネイティブJSに書き換えたいと考えているのですが、変化してくれません。
//jQuery javascript $(".menu-btn").click(function(){ $(".navbar .menu").toggleClass("active"); }); //normal javascript document.getElementsByClassName("menu-btn").addEventListener("click",function(){ document.getElementsByClassName("navbar menu").classList.toggle(".active"); });
上記のコードは、menu-btnをクリックすると以下のクラス名(.navbar .menu)に
.navbar .menu{ background: black; position: fixed; height: 100vh; width: 100%; top: 0; left: -100%; text-align: center; padding-top: 80px; }
以下のように.activeを追加してメニューが横からスライドしてくるアニメーションを付けるものです。
.navbar .menu.active{ left: 0; }
しかし、jQueryを書き換えてもアニメーションは起こりませんでした。これは何が足りないのでしょうか。どう書き換えればよいのでしょうか。
以下HTML、CSSとJSのコードです。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Portfolio1</title> <link rel="stylesheet" href="style.css"> <script src="https://kit.fontawesome.com/ff832bf6f3.js" crossorigin="anonymous"></script> </head> <body> <!-- Navigate section start --> <nav class="navbar"> <div class="max-width"> <div class="logo"><a href="#">Portfo<span>lio.</span></a></div> <ul class="menu"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Skills</a></li> <li><a href="#">Contact</a></li> </ul> <div class="menu-btn"> <i class="fas fa-bars"></i> </div> </div> </nav> <!-- Home section start --> <section class="home" id="home"> <div class="max-width"> <div class="home-content"> <div class="text-1">Hello, my name is</div> <div class="text-2">Ogino Kaito</div> <div class="text-3">I'm a <span>Student</span></div> </div> </div> </section> <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> <script src="main.js"></script> </body> </html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&family=Ubuntu:wght@400;500;700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; text-decoration: none; } .max-width{ max-width: 1300px; padding: 0 80px; margin: auto; } /* navber style */ .navbar{ position: fixed; width: 100%; padding: 30px 0; font-family: "Ubuntu", sans-serif; transition: all 0.3s ease; } .navbar.sticky{ padding: 15px 0; background: crimson; } .navbar .max-width{ display: flex; align-items: center; justify-content: space-between; } .navbar .logo a{ font-size: 35px; font-weight: 600; color: white; } .navbar .logo a span{ color: crimson; transition: all 0.3s ease; } .navbar.sticky .logo a span{ font-size: 35px; font-weight: 600; color: white; } .navbar .menu li{ display: inline-block; list-style: none; } .navbar .menu li a{ color: white; font-size: 18px; font-weight: 500; margin-left: 25px; transition: color 0.3s ease; } .navbar .menu li a:hover{ color: crimson; } .navbar.sticky .menu li a:hover{ color: white; } /* menu button style */ .menu-btn{ color: white; font-size: 23px; cursor: pointer; display: none; } /* home style */ .home{ display: flex; background: url("img/black.jpg") no-repeat center; background-size: cover; background-attachment: fixed; height: 100vh; color: white; min-height: 500px; font-family: "Ubuntu", sans-serif; } .home .max-width{ margin: auto 0 auto 40px; } .home .home-content .text-1{ font-size: 27px; } .home .home-content .text-2{ font-size: 75px; font-weight: 600; margin-left: -3px; } .home .home-content .text-3{ font-size: 40px; margin: 0 5px; } .home .home-content .text-3 span{ color: crimson; font-weight: 500; } /* responsive media query style */ @media (max-width: 947px){ .max-width{ padding: 0 50px; } .menu-btn{ display: block; z-index: 999; } .navbar .menu{ background: black; position: fixed; height: 100vh; width: 100%; top: 0; left: -100%; text-align: center; padding-top: 80px; } .navbar .menu.active{ left: 0; } .navbar .menu li{ display: block; } .navbar .menu li a{ display: inline-block; margin: 20px 0; font-size: 25px; } }
// navvar script window.addEventListener("scroll", function(){ var navbar = document.querySelector(".navbar"); navbar.classList.toggle("sticky", window.scrollY > 20); }); //jQuery javascript $(".menu-btn").click(function(){ $(".navbar .menu").toggleClass("active"); }); //normal javascript document.getElementsByClassName("menu-btn").addEventListener("click",function(){ document.getElementsByClassName("navbar menu").classList.toggle(".active"); });
回答2件
あなたの回答
tips
プレビュー