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);
}
}));
Related
I've tried making a dropdown menu for a website that I'm working on and it worked, but I don't know how I can make it disappear after I press somewhere else. Can someone help me pls?
I tried searching on google but didn't succeed unfortunately
<script>
function toogleRESTAURANT(){
let restaurant = document.getElementById("restaurant")
restaurant.classList.toggle('active')
}
</script>
<script>
function toggleBAR(){
let bar = document.getElementById("bar")
bar.classList.toggle('active')
}
</script>
<script>
function togglePrivate(){
let dining = document.getElementById("dining")
dining.classList.toggle('active')
}
</script>
<nav>
<div>
<button onclick="toogleRESTAURANT()">RESTAURANT</button>
<ul id="restaurant">
<li>BOOK NOW</li>
<li>MENUS</li>
<li>AFTENOON TEA</li>
<li>ROSE TASTING TRIO</li>
<li>FAQ</li>
</ul>
</div>
<div>
<button class="btnBAR" onclick="toggleBAR()">BAR</button>
<ul id="bar">
<li>BOOK NOW</li>
<li>MENUS</li>
<li>ROSE TASTING TRIO</li>
</ul>
</div>
<div>
<button class="privateD" onclick="togglePrivate()">PRIVATE DINING</button>
<ul id="dining">
<!-- <li>DOAR TEST</li> -->
</ul>
</div>
</nav>
You can add an click event handler on the document object, so that you can detect when a user clicks anywhere else on the webpage and then hide the dropdown (in this case remove the "active" class from the dropdown classList)
Check on this code snippet.
document.onclick = function (event) {
if (bar.classList.contains("active") && !event.target.matches(".btnBAR")) {
bar.classList.remove("active");
}
if (
restaurant.classList.contains("active") && !event.target.matches(".btnRESTAURANT")
) {
restaurant.classList.remove("active");
}
if (
dining.classList.contains("active") &&
!event.target.matches(".privateD")
) {
bar.classList.remove("active");
}
};
You don't really need separate functions for each menu. When menu was opened, register a click event listener on window or document object, once it triggered, close the menu and remove listener. This is not the best approach overall though.
var opened = null;
function toggle(event)
{
event.stopPropagation(); //make sure we don't get "double click" event
const button = event.target;
//close previous menu
if (opened && opened.target !== button)
toggle(opened);
if (button && button.tagName == "BUTTON")
{
const node = button.parentNode.querySelector("ul");
node.classList.toggle('active');
//if menu opened, register click event on window
if (node.classList.contains("active"))
{
window.addEventListener("click", toggle);
//store current event so we can use it later to close menu when clicked anywhere
opened = event;
}
}
else //remove window click event listener and clear opened variable
opened = window.removeEventListener("click", toggle);
}
nav > div > ul
{
display: none;
}
.active
{
display: block;
}
<nav>
<div>
<button onclick="toggle(event)">RESTAURANT</button>
<ul id="restaurant">
<li>BOOK NOW</li>
<li>MENUS</li>
<li>AFTENOON TEA</li>
<li>ROSE TASTING TRIO</li>
<li>FAQ</li>
</ul>
</div>
<div>
<button class="btnBAR" onclick="toggle(event)">BAR</button>
<ul id="bar">
<li>BOOK NOW</li>
<li>MENUS</li>
<li>ROSE TASTING TRIO</li>
</ul>
</div>
<div>
<button class="privateD" onclick="toggle(event)">PRIVATE DINING</button>
<ul id="dining">
<!-- <li>DOAR TEST</li> -->
</ul>
</div>
</nav>
I am trying to create a hamburger menu. When I click on the three lines I don't get the 'x'. I think I have the classes named correctly. If you need more code I can provide that also.
$('.js--nav-icon').click(function() {
var nav = $('.js--main-nav');
var icon = $('.js--nav-icon');
nav.slideToggle(200);
if (icon.hasClass('.menu-outline')) {
icon.addClass('.close-outline');
icon.removeClass('.menu-outline');
} else {
icon.addClass('.menu-outline');
icon.removeClass('.close-outline');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<img src="Resources/img/logo-white.png" alt="Omnifood logo" class="logo">
<img src="Resources/img/logo.png" alt="Omnifood logo" class="logo-black">
<ul class="main-nav js--main-nav">
<li>Food delivery</li>
<li>How it works</li>
<li>Our cities</li>
<li>Sign up</li>
</ul>
<ion-icon class="mobile-nav-icon js--nav-icon" name="menu-outline"></ion-icon>
</div>
You should not use dot(.) in jQuery hasClass,addClass,removeClass functions. remove those dots:
if (icon.hasClass('menu-outline')) {
icon.addClass('close-outline');
icon.removeClass('menu-outline');
}
else {
icon.addClass('menu-outline');
icon.removeClass('close-outline');
}
Please remove (.) as below:
icon.addClass('close-outline');
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)
Im having a problem using href/anchor scrolling with angularjs. Once I click the h ref link it always links to the index page sample like this http://xxxxxx/#!/index#sec-menu not sure what I'm doing wrong.
Thanks for the assistance.
HTML
<div class="nav-holder main-menu">
<nav>
<ul>
<li>
Menu
</li>
</ul>
</nav>
</div>
<span class="fw-separator" id="sec-menu"></span>
not sure if this is the cause but I have something like this in my app.js
.otherwise({
redirectTo: '/index'
});
<div class="nav-holder main-menu">
<nav>
<ul>
<li>
<a ng-href="#/?scroll=sec-menu">My element</a>
</li>
</ul>
</nav>
</div>
<span class="fw-separator" id="sec-menu"></span>
change the run code in the module declaration in app.js like so:
app.run(function($location, $rootScope) {
$rootScope.$watch(function() { return $location.search() }, function(search) {
var po= 0;
if (search.hasOwnProperty('scroll')) {
var $target = $('#' + search.scroll);
po= $target.offset().top;
}
$("body,html").animate({scrollTop: po}, "slow");
});
})
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;
}