I made a hamburger menu, and when I click on the links it closes it. Also has a logo which disappears when I click on the hamburger button.
The webpage has a mobile and a desktop computer version that doesn't have a hamburger menu.
But I have a problem. On the Desktop version, if I click on the links the Logo disappears as well.
I know that it does it because the hamburger-close-after-click event triggers it.
But I don't know how I could change it to make it work well.
jQuery Code
const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelectorAll('.nav-links li');
var mylogo = document.getElementById("myLogo");
hamburger.addEventListener('click', () => {
navLinks.classList.toggle('open');
links.forEach(link =>{
link.classList.toggle("fade");
});
});
//logo-toggle
$(document).ready(function(){
$('.hamburger').click(function() {
$('.logo-container').toggle().delay( 800 );
});
});
// hamburger-close-after-click event
$( '.nav-links li a' ).on("click", function(){
$('#hamburgerID').click();
});
HTML Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<nav>
<div class="hamburger" id="hamburgerID">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
</nav>
<header class="header" id="myHeader">
<div class="logo-container" id="myLogo">
<img src="./img/logo.png" alt="logo"/>
</div>
<nav>
<ul class="nav-links">
<li><a class="nav-link" href="#details">DETAILS</a></li>
<li><a class="nav-link" href="#description">DESCRIPTION</a></li>
<li><a class="nav-link" href="#aboutus">ABOUT US</a></li>
</ul>
</nav>
The problem with pictures
[
You can use JQuery.toggleClass() to add and remove CSS classes, instead of using JQuery.toggle(), e.g.:
$('.hamburger').click(function() {
$('.logo-container').toggleClass('open');
})
And then, in the CSS file, define the visibility depending on the screen size, e.g.:
.logo-container { display: 'none' }
.logo-container.open { display: 'block' }
#media (min-width: 1025px) {
.logo-container { display: 'block' }
}
(Note: You will want to decide yourself how to identify the appropriate size to hide and show the elements. See also e.g.: how-to-target-desktop-tablet-and-mobile)
Related
I am using the mustard css framework but I am having an issue when resizing my browser. The navigation bar is supposed to drop into a hamburger, the appears but when i click it nothing appears within it. I copied and pasted the mustard exact nav bar but still doesn't work. Am i missing something else I have to link.
My Navigation Bar
<nav class="mynav">
<div class="nav-container">
<div class="nav-logo">
<div>
<img class="mylogo" src="imgs/abs.png" alt="">
</div>
<a class="myfont" href="index.php">Abs'olute Fitness</a>
</div>
<a class="mobile-menu-toggle"></a>
<ul class="mobile-menu menu">
<li>About</li>
<li>Contact</li>
<li>Login</li>
</ul>
</div>
</nav>
Mustard Navigation Bar
<nav>
<div class="nav-container">
<div class="nav-logo">
mustard
</div>
<ul class="nav-links">
<li><a class="active" href="/docs/installation">Docs</a></li>
<li>GitHub</li>
<li>Support</li>
</ul>
<a class="mobile-menu-toggle"></a>
<ul class="mobile-menu menu">
<li>Docs</li>
<li>GitHub</li>
<li>Support</li>
</ul>
</div>
</nav>
You need some javascript to toggle appearance. There's jquery example in repository, I'm using vanilla JS for that:
const hide = ((elem) => {
elem.style.display = 'none';
});
const show = ((elem) => {
elem.style.display = 'block';
});
const toggle = document.querySelector('.mobile-menu-toggle');
const menu = document.querySelector('.mobile-menu');
toggle.addEventListener('click', ((e) => {
if (menu.style.display !== 'block') {
e.preventDefault();
e.stopPropagation();
show(menu);
}
}));
document.body.addEventListener('click', ((e) => {
if (menu.style.display === 'block') {
e.preventDefault();
e.stopPropagation();
hide(menu);
}
}));
I have a web page with a navbar and map div. Everything is great but there is a scroll bar on the page and you must scroll to see the full map. I would like the full map div to show up on the screen without any scrolling. How do I accomplish this? Thank you
HTML:
<nav class="default" role="navigation">
<div class="nav-wrapper">
NTN Trails
<a href="#" data-activates="mobile-demo" class="button-collapse show-
on-large"><i class="material-icons">menu</i></a>
<ul class="right hide-on-med-and-down">
</ul>
<ul class="side-nav" id="mobile-demo">
<li>Login</li>
<li>Sign up</li>
</ul>
</div>
</nav>
<script>
$('.button-collapse').sideNav({
menuWidth: 240, // Default is 240
edge: 'left', // Choose the horizontal origin
closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor
}
);
</script>
<!--put your initial page content here-->
<div id="map"></div>
CSS:
html, body, #map {
height: 100%;
width: 100%;
}
#media only screen and (min-width: 993px){
nav a.button-collapse {
display: block;
}
}
I am currently building a website which contains a accordion menu.
My problem is the following, when I click the desired item which has to collapse it does collapse but it goes right back to uncollapsed after it collapsed.
$(document).ready(function(){
$(".set > a").on("click", function(){
if($(this).hasClass('active')){
$(this).removeClass("active");
$(this).siblings('.content').slideUp(200);
$(".set > a i").removeClass("fa-minus").addClass("fa-plus");
}else{
$(".set > a i").removeClass("fa-minus").addClass("fa-plus");
$(this).find("i").removeClass("fa-plus").addClass("fa-minus");
$(".set > a").removeClass("active");
$(this).addClass("active");
$('.content').slideUp(200);
$(this).siblings('.content').slideDown(200);
}
});
});
Here is the code (jsfiddle)
This code used to work perfectly fine, but for some reason it broke and doesn't work anymore.
I hope someone can help me out.
I think somewhere along the lines you have swapped your accordion to start open (ie removed the styles that initially hide it) so the following lines in your else both opens and closes it:
$('.content').slideUp(200);
$(this).siblings('.content').slideDown(200);
If you are starting with your accordion open, then you need to start with the active class on your link:
$(document).ready(function() {
$(".set > a").on("click", function() {
var link = $(this);
if (link.hasClass('active')) {
link.next('.content').slideUp(200, function() {
link.children('i').removeClass("fa-minus").addClass("fa-plus");
link.removeClass("active");
});
} else {
link.next('.content').slideDown(200, function() {
link.children('i').removeClass("fa-plus").addClass("fa-minus");
link.addClass("active");
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="accordion-container">
<div class="set">
Vloeren <i class="fa fa-plus"></i>
<div class="content">
<ul class="submenu">
<li class="submenu-item">Witte vloer</li>
<ul class="submenu-two">
<li class="submenu-item">Witte vloer</li>
</ul>
<li class="submenu-item">Zwarte vloer</li>
<li class="submenu-item">Grijze vloer</li>
<li class="submenu-item">Paarse vloer</li>
</ul>
</div>
</div>
</div>
I have also cleaned up your jQuery and fixed your else
I have a responsive nav bar. When the screen size decreases a little, the last li goes to the next line under other li's. What should I do to fix it so it doesn't move to the next line under the other menu elements?
HTML codes:
<nav class="cd-main-nav-wrapper mainNav">
<ul class="cd-main-nav">
<li style="margin-right:none;" class="logoNavPos">
<a href="index.html">
<img src="pic/logo1.jpg" alt="" class="desktopLogo">
</a>
</li>
<li class="current_page_item">
خانه </li>
<li>مزایای بیمه عمر</li>
<li>بیمه عمر و سرمایه گذاری</li>
<li>پرسش های متداول</li>
<li>درباره ما</li>
<li style="display:none;" class="mobileSwLang">
English version</li>
</ul>
</nav>
https://jsfiddle.net/MelodyHajian/4ntL3m9o/
give me advices plz.
jquery may help
here is a jsfiddle i played with......
$(window).resize(function() {
var windowsize = $(window).width();
if (windowsize > 767) {
$('.nav').show();
} else {
$('.nav').hide();
$('.toggle').removeClass('open');
}
})
$('.toggle').click(function(event) {
$(this).toggleClass('open');
if ($(this).hasClass('open')) {
$('.nav').show();
} else {
$('.nav').hide();
}
})
http://jsfiddle.net/GlynneT/2pv9pkb0/
I'm using iScroll for a horizontal, fixed position menu. It works great, however I'm unable to vertically scroll the rest of the page in a mobile browser. It works fine in a pc/mac resized browser window, just not on mobile. I've tried Safari on iPhone and Chrome and Dolphin on Android.
Any ideas how I can get the page to scroll too?
I've played with the vScroll, hScroll, etc options but haven't been able to solve the issue:
<nav id="mainNav">
<ul class="pagesIcons bottom">
<li style="background-color:#000000" class="active selected current youarehere">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/002.png">
</div>
<div class="buttonText">
Homepage
</div>
</a>
</li>
<li style="background-color:#383838" class="">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/002.png">
</div>
<div class="buttonText">
Gallery
</div>
</a>
</li>
<li style="background-color:#5c5c5c" class="">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/004.png">
</div>
<div class="buttonText">
Events
</div>
</a>
</li>
........
</ul>
</nav>
I'm initialising it with:
var myScroll;
function loaded() {
myScroll = new iScroll('mainNav', {
bounce: false
});
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
Solved. I just had to remove:
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
Pretty obvious. This was preventing the default event on 'touchmove'. Durrrrrrr
You can fix this issue by applying this jquery, In place of ClassName you have to add the name of wrapper class in which you want scrolling, If you still face problem let me know I will try to help more.
$('.ClassName').bind('touchmove', function(e){
e.stopPropagation();
});