简体   繁体   English

在移动设备上,我希望在单击 ID 时关闭导航栏

[英]On mobile I want my navbar to close when I click an ID

I have a problem with my navbar.我的导航栏有问题。 I want to select an #id (page) on my mobile device, reach that side of the page and then close the .nav-wrapper which holds all the ul li's, the one that was opened to view all the pages (id's).我想在我的移动设备上选择一个#id(页面),到达页面的那一侧,然后关闭包含所有 ul li 的 .nav-wrapper,打开以查看所有页面(id)的那个。

I've tried something but it didn't work.我试过一些东西,但没有用。 Hope you understood my question and also I hope that you can help me.希望你理解我的问题,也希望你能帮助我。

 $(document).ready(function(){ $(window).scroll(function(){ if($(document).scrollTop()>130){ $(".nav-btn").addClass("fundal") } else{ $(".nav-btn").removeClass("fundal") } }); }); window.addEventListener('load', ()=> { const preload = document.querySelector('.preload'); preload.classList.add('preload-finish'); }); $("ul").click(function () { $(".nav-wrapper").toggleClass(".nav-btn"); })
 /* NAVBAR */ .navigatie{ background-color:transparent; width:100%; position: absolute;; z-index: 99; margin:0px; padding:0px; } .logo { float: left; padding: 8px; margin-left: 40px; margin-top: 8px; } .navbar-brand{ color:white !important; } .navbar-brand img{ width: auto; height: 100px; margin:-32px 0px -25px 0px; } nav ul { float: right; list-style-type: none; padding-top:5px; margin-right:20px; } nav ul li { float: left; } nav ul li:not(:first-child) { margin-left: 48px; } nav ul li:last-child { margin-right: 24px; } nav ul li a { display: inline-block; outline: none; color: #fff; text-transform: uppercase; text-decoration: none; font-size: 16px; letter-spacing: 1.1px; font-weight: 700; } nav ul li a:hover{ color:#fff; text-decoration: underline; } /* FUNDAL JS */ .fundal{ background:#e04f45 !important; box-shadow:1px 2px 4px 0px #00000075 !important; } .fundal .nav-btn { background-color:#e04f45; border-radius: 50%; box-shadow:1px 2px 4px 0px #00000075; } .fundal .nav-btn i{ background: #fff; border-radius: 2px; } #nav:checked + .nav-btn { transform: rotate(45deg); background-color: #e04f45; } #nav:checked + .nav-btn i { background: #fff; transition: transform 0.2s ease; } #nav:checked + .nav-btn i:nth-child(1) { transform: translateY(6px) rotate(180deg); } #nav:checked + .nav-btn i:nth-child(2) { opacity: 0; } #nav:checked + .nav-btn i:nth-child(3) { transform: translateY(-6px) rotate(90deg); } #nav:checked ~ .nav-wrapper { z-index: 9990; display:block } #nav:checked ~ .nav-wrapper ul li a { opacity: 1; transform: translateX(0); } .hidden { display: none; } /* MEDIA SCREEN NAVBAR PTR TLF */ @media only screen and (max-width: 991px){ .navigatie{ background-color:transparent; width:100%; position: absolute; z-index: 99; margin:0px; padding:0px; } .navbar-brand img{ height:100px; margin:-20px 0px 0px 0px; } .nav-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; background: #fff; display:none; transition: all 0.2s ease; } .nav-wrapper ul { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; } .nav-wrapper ul li { display: block; float: none; width: 100%; text-align: center; padding-top:10px; margin-bottom: 10px; } .nav-wrapper ul li:not(:first-child) { margin-left: 0; } .nav-wrapper ul li a { padding: 10px 24px; opacity: 0; color: #000; font-size: 14px; font-weight: 600; letter-spacing: 1.2px; transform: translateX(-20px); transition: all 0.2s ease; } .nav-btn { position: fixed; right: 30px; top: 28px; display: block; width: 47px; height: 46px; cursor: pointer; z-index: 9999; border-radius: 50%; } .nav-btn i { display: block; width: 20px; height: 2px; background: #fff; border-radius: 2px; margin-left: 14px; } .nav-btn i:nth-child(1) { margin-top: 16px; } .nav-btn i:nth-child(2) { margin-top: 4px; opacity: 1; } .nav-btn i:nth-child(3) { margin-top: 4px; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="navigatie"> <nav> <input type="checkbox" id="nav" class="hidden"> <label for="nav" class="nav-btn"> <i></i> <i></i> <i></i> </label> <div class="nav-wrapper"> <ul> <li><a href="index.php">Acasă</a></li> <li><a href="#despre">Despre</a></li> <li><a href="#studii">Studii</a></li> <li><a href="#abilitati">Skills</a></li> <li><a href="#contact">Contact</a></li> </ul> </div> </nav> </div>

You could do that by changing a small thing in your JavaScript.你可以通过改变 JavaScript 中的一个小东西来做到这一点。 Now the menu is showing when the checkbox is checked, you need to make sure that the checkbox gets unchecked when clicking a link.现在菜单在复选框被选中时显示,您需要确保单击链接时复选框被取消选中。 I've updated the JS in your 'click' function我已经在你的“点击”功能中更新了 JS

Tip: try to avoid doing one thing with Vannila JS and the other thing with jQuery提示:尽量避免用 Vannila JS 做一件事,用 jQuery 做另一件事

 $(document).ready(function () { $(window).scroll(function () { console.log($(document).scrollTop()); if ($(document).scrollTop() > 130) { $(".nav-btn").addClass("fundal"); console.log("add class", $(".nav-btn").hasClass("fundal")); } else { $(".nav-btn").removeClass("fundal"); console.log("remove class"); } }); }); window.addEventListener("load", () => { const preload = document.querySelector(".preload"); preload.classList.add("preload-finish"); }); // Updated the selector $(".nav-wrapper li a").click(function () { $(".nav-wrapper").toggleClass(".nav-btn"); // Uncheck your checkbox $("#nav").prop("checked", false) });
 .page { background: #f00; height: 500px; margin-bottom: 10px; } /* NAVBAR */ .navigatie { background-color: transparent; width: 100%; position: absolute; z-index: 99; margin: 0px; padding: 0px; } .logo { float: left; padding: 8px; margin-left: 40px; margin-top: 8px; } .navbar-brand { color: white !important; } .navbar-brand img { width: auto; height: 100px; margin: -32px 0px -25px 0px; } nav ul { float: right; list-style-type: none; padding-top: 5px; margin-right: 20px; } nav ul li { float: left; } nav ul li:not(:first-child) { margin-left: 48px; } nav ul li:last-child { margin-right: 24px; } nav ul li a { display: inline-block; outline: none; color: #fff; text-transform: uppercase; text-decoration: none; font-size: 16px; letter-spacing: 1.1px; font-weight: 700; } nav ul li a:hover { color: #fff; text-decoration: underline; } /* FUNDAL JS */ .fundal { background: #e04f45 !important; box-shadow: 1px 2px 4px 0px #00000075 !important; } .fundal.nav-btn { background-color: #e04f45; border-radius: 50%; box-shadow: 1px 2px 4px 0px #00000075; } .fundal.nav-btn i { background: #fff; border-radius: 2px; } #nav:checked + .nav-btn { transform: rotate(45deg); background-color: #e04f45; } #nav:checked + .nav-btn i { background: #fff; transition: transform 0.2s ease; } #nav:checked + .nav-btn i:nth-child(1) { transform: translateY(6px) rotate(180deg); } #nav:checked + .nav-btn i:nth-child(2) { opacity: 0; } #nav:checked + .nav-btn i:nth-child(3) { transform: translateY(-6px) rotate(90deg); } #nav:checked ~ .nav-wrapper { z-index: 9990; display: block; } #nav:checked ~ .nav-wrapper ul li a { opacity: 1; transform: translateX(0); } .hidden { display: none; } /* MEDIA SCREEN NAVBAR PTR TLF */ @media only screen and (max-width: 991px) { .navigatie { background-color: transparent; width: 100%; position: absolute; z-index: 99; margin: 0px; padding: 0px; } .navbar-brand img { height: 100px; margin: -20px 0px 0px 0px; } .nav-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; background: #fff; display: none; transition: all 0.2s ease; } .nav-wrapper ul { position: absolute; top: 50%; transform: translateY(-50%); width: 100%; } .nav-wrapper ul li { display: block; float: none; width: 100%; text-align: center; padding-top: 10px; margin-bottom: 10px; } .nav-wrapper ul li:not(:first-child) { margin-left: 0; } .nav-wrapper ul li a { padding: 10px 24px; opacity: 0; color: #000; font-size: 14px; font-weight: 600; letter-spacing: 1.2px; transform: translateX(-20px); transition: all 0.2s ease; } .nav-btn { position: fixed; right: 30px; top: 28px; display: block; width: 47px; height: 46px; cursor: pointer; z-index: 9999; border-radius: 50%; } .nav-btn i { display: block; width: 20px; height: 2px; background: #fff; border-radius: 2px; margin-left: 14px; } .nav-btn i:nth-child(1) { margin-top: 16px; } .nav-btn i:nth-child(2) { margin-top: 4px; opacity: 1; } .nav-btn i:nth-child(3) { margin-top: 4px; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="preload"></div> <div class="navigatie"> <nav> <input type="checkbox" id="nav" class="hidden"> <label for="nav" class="nav-btn"> <i></i> <i></i> <i></i> </label> <div class="nav-wrapper"> <ul> <li><a href="#home">Acasă</a></li> <li><a href="#despre">Despre</a></li> <li><a href="#studii">Studii</a></li> <li><a href="#abilitati">Skills</a></li> <li><a href="#contact">Contact</a></li> </ul> </div> </nav> </div> <div class="page" id="home"></div> <div class="page" id="despre"></div> <div class="page" id="studii"></div> <div class="page" id="abilitati"></div> <div class="page" id="contact"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击其中一个部分时如何关闭导航栏? - How to close my navbar when I click on one of the sections? 我想向我的导航栏添加一个类,然后当我再次单击时我想将其删除 - I want to add a class to my navbar and then when I click again I want to remove it 单击导航栏外部时如何关闭导航栏? - How to close navbar when I click outside the navbar? 单击<a>链接</a>时如何关闭导航栏<a>?</a> - How do i get the navbar to close when i click on a <a> link? 有没有办法在我点击屏幕的任何地方关闭我的活动导航栏? - Is there a way to close my active navbar wherever I click on screen? 如何通过点击关闭我的twitter bootstrap导航栏? - How do I get my twitter bootstrap navbar to close on click? 当我单击页面上的其他任何位置时,我希望我的手风琴关闭 - I want my accordion to close when I click anywhere else on the page 当我仅在移动视图中单击汉堡时,粘性导航栏消失 - Sticky Navbar Disapears When I Click On Hamburger in Mobile View Only 当我在给定位置单击应用程序上的“关闭”按钮时,我的inapp浏览器路径未关闭。href=&#39;mobile / close&#39; - my inapp browser path is not closing when i click on Close button on app given location.href = 'mobile/close' 我希望我的导航栏在单击移动尺寸切换时与内容重叠 - I want my navigation bar overlapping the content when click the toggle on mobile size
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM