I have created a responsive menu that can opens and collabses when i push the open menu.
But the problem is that when i resize the screen it is auto opened when it hits 680 px or lower and if i then collapse the menu when i'm mobile size and try to resize it to desktop again(over 680px) the menu stays hidden.
html code
<img src="assets/img/nav-icon.png" alt="Menu">
<div class="bar">
<div class="lang">
<div class="langDK"></div>
<div class="langUK"></div>
</div>
<ul class="navigation">
<img src="assets/img/logo.png" alt="">
<li class="item">Home</li>
<li class="item">Products</li>
<li class="item">Work</li>
<li class="item">About</li>
<li class="item">Contact</li>
</ul>
</div>
jquery
$(function(){
var pull = $('#pull');
menu = $('.bar .item');
w = $(window).width();
if(w > 680 ) {
menu.show()
}
if(w < 680){
menu.hide();
}
$(pull).on('click', function(e) {
e.preventDefault();
menu.slideToggle();
});
});
i also have some css and media querys but dont think the problem lies there.
Set up the event listener for the window's resize:
$(window).on('resize', function(){
var menu = $('.bar .item');
var w = $(window).width();
if(w >= 680 ) {
menu.show()
}else{
menu.hide();
}
})
If you'd like to hide the li items by default, add the CSS class as below:
li.item {display:none;}
try this one
$(function() {
var pull = $('#pull');
menu = $('.navigation .dropdown');
menuHeight = menu.height();
$(pull).on('click', function(e) {
e.preventDefault();
menu.slideToggle();
});
$(window).resize(function(){
var w = $(window).width();
if(w > 320 && menu.is(':hidden')) {
menu.removeAttr('style');
}
});
});
Html
<div class="navigation">
<ul class="nav pull-right group dropdown">
<li class="nav-line-image"></li>
<li>Home</li>
<li>About Us</li>
<li>Design Gallery</li>
<li>Designer Collection</li>
<li>Silver Collection</li>
<li>Promotion/Events</li>
<li class="active">Branch Details</li>
<li>Contact Us</li>
<li class="nav-line-image"></li>
</ul>
</div><!--navigation end-->
css for mobile and tabs
.nav{
display : none;
position: relative;
}
#pull {
position: absolute;
display : block;
width : 17px;
height : 20px;
color : #5a504c;
margin : 15px 0 0 20px;
background : #ccc;
text-decoration :none;
}
Related
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)
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);
}
}));
how to make responsive sticky navbar working in desktop using uikit
here is what i have tried
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div uk-sticky="sel-target: .uk-navbar-container; cls-active: uk-navbar-sticky; bottom: #transparent-sticky-navbar" style="background-color: rgba(0,0,0,.4)">
<nav class="uk-navbar-container uk-navbar-transparent uk-height-match" uk-navbar style="position: relative; z-index: 980;height: 77px;">
<div class="uk-navbar-center">
<ul class="uk-navbar-nav">
<li class="uk-active">Active</li>
<li>
Parent
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li class="uk-active">Active</li>
<li>Item</li>
<li>Item</li>
</ul>
</div>
</li>
<li>Item</li>
</ul>
</div>
</nav>
</div>
the navbar is working on all display views but i don't want it to work on tablet & mobile view
how can i achieve that ?
You can make a listener for window resize and when the window width is as a mobile size, hide your navbar, I made it once like this in your parent component :
data: function() {
return {
windowWidth: 0,
}
},
mounted() {
this.$nextTick(function() {
window.addEventListener('resize', this.getWindowWidth);
//Init
this.getWindowWidth()
})
},
methods: {
getWindowWidth(event) {
this.windowWidth = document.documentElement.clientWidth;
},
},
beforeDestroy() {
window.removeEventListener('resize', this.getWindowWidth);
}
And in your template for example (pseudo code):
<template>
<navbar v-if="windowWidth > 800px"></navbar>
</template>
Hope it helps
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 trying to make a bootstrap dropdown menu open/close on hover, but with a delay of 250ms for desktop only.
JavaScript:
$(document).ready(function() {
if ($(window).width() > 767) {
$(".dropdown-toggle").click(function() {
return false;
});
var hoverTimeout;
$(".dropdown-toggle, .dropdown-menu").hover(function() {
hoverTimeout = setTimeout(function(){
$(this).parent(".dropdown").addClass("open");
}, 250);
},function() {
clearTimeout(hoverTimeout);
setTimeout(function() {
$(this).parent(".dropdown").removeClass("open");
}, 250);
});
}
});
HTML
It's the normal bootstrap structure suggested in the documentation:
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="happyMenu">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle"
data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">
Title
</a>
<ul class="container dropdown-menu">
<li>
<a href="#">
List Title
</a>
<ul>
<li>
<a href="#">
List Item
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
The jQuery code above does not add the open class to the parent of dropdown-toggle.
Here is the solution I came up with using JavaScript setTimeout with no changes in HTML structure:
$(document).ready(function() {
if ($(window).width() > 767) {
$(".dropdown-toggle").click(function() {
return false;
});
var hoverTimeout;
$(".dropdown-toggle").hover(function() {
var element = $(this).parent(".dropdown");
hoverTimeout = setTimeout(function(){
element.addClass("open");
}, 250);
},function() {
clearTimeout(hoverTimeout);
var element = $(this).parent(".dropdown");
hoverTimeout = setTimeout(function() {
element.removeClass("open");
}, 250);
$(".dropdown-menu").hover(function(){
clearTimeout(hoverTimeout);
},function(){
setTimeout(function() {
element.removeClass("open");
}, 250);
});
});
}
});