I have a nav-bar which has display:none by default. When a user clicks on a hamburger icon at the top-right corner, I want to display it. What I've done is,
<nav id="navigation-bar">
....
</nav>
Javascript:
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.add('show-btn');
});
In my css, the below style is not working
.show-btn{
display:block
}
But when I add nav. in front of it, it works
nav.show-btn{
display:block
}
ps: I'm using sass
Add a class to your nav tag
So that your Code should look like this
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.add('show-btn');
});
.navbar{
display:none;
}
.show-btn{
display:block
}
<nav id="navigation-bar" class="navbar">
<ul>
<li>Test</li>
</ul>
</nav>
<button id="menu-cta">btn</button>
please don't add css to #navigation-bar{} otherwise it will have the highest specificty.
You can learn more here
CSS Specificity
Another route is to use toggle() so the user can open and close your navigation.
const menuBtn = document.getElementById("menu-cta"), //the menu icon
navBar = document.getElementById("navigation-bar");
menuBtn.addEventListener('click',()=>{
navBar.classList.toggle('hide');
});
.hide {
display: none
}
<button id="menu-cta">...</button>
<nav id="navigation-bar" class="hide">
<ul>
<li>Test</li>
</ul>
</nav>
Related
I have an nav and a button. Now nav can work properly when resizing the window of the browser, however the button will override the 's last item when resizing the window to a certain width, so how to solve it?
Normal thing:
Overridding:
What I want is: When resizing the width to a smaller size, it will make the button(s) to the next new line.
<nav aria-label="primary">
<ul>
<li>...</li>
....
</ul>
</nav>
<button class="dark-theme-switcher" type="button" aria-label="Toggle dark/light mode"></button>
Add your button into the li property which is available under ul property. add a new class for that button and add a margin-left to that class (button) as you wish.
.nav-btn{
margin-left: (xx)px
}
<nav aria-label="primary">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact Us</li>
<li><button class="dark-theme-switcher nav-btn"...</button></li>
</ul>
</nav>
Or use nav component which is available in bootstrap components - https://getbootstrap.com/docs/5.1/components/navbar/
I am trying to have an anchor inside a list item to be changing colors when the mouse is hovering over. It is not working properly for PORTFOLIO and CONTACT (when I hover over ABOUT, CONTACT changes color) but it not working at all for HOME and ABOUT (HOME and ABOUT do not change color at all). Why might that be?
Here is my CSS code:
li a:hover{color: #E3872D;}
And here my HTML code:
<div class="leftpart_wrap">
<ul class="navbar">
<li>Home</li>
<li>About</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
<div class="leftpart_bottom">
<ul id="icons">
<!--Icons go here and their hovering attribute works perfectly-->
</ul>
</div>
</div>
It could be a few things. Your code is correct but probably has some overwriting style in your CSS. Try using more specific CSS to see if it resolves:
.navbar li a:hover{color: #E3872D;}
I am trying to introduce new class for specific div element using nth-child() pseudo-class but it is not working!
Am I doing something wrong?
Note that menu, container, newClass are already defined in CSS section and I'm not showing them here.
Here is what I am trying:
$(function() {
$("button").click(function {
$("div:nth-child(2)").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<lu>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</lu>
</div>
</div>
There are lots of mistakes in your code. Which div are you want to add a class newClass? I think your code should be like this
$(function() {
$("button").on('click', function() {
$(".menu").toggleClass("newClass");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Toggle CSS</button>
<div class="container">
<div class="menu">
<ul>
<li>option 1</li>
<li>option2</li>
<li>option 3</li>
</ul>
</div>
</div>
My mistake
I should not use
<script>
$("div:nth-child(2)").toggle Class("newClass");
</script>
this to introduce new class for div element, with the class menu , as it is not a second child of div . What given below is though working fine for me:
Correction
<script>
$("div.menu").toggleClass("newClass");
</script>
That is: to specify div element(whose class is going to be toggled) by tagging its class menu , to call jQuery for div with the class menu .
Also by mistake parentheses () were missing.
I recently get task and they explain this task in the following way
The navigation must be styled using the :hover pseudo class, while the active menu point must use the body class
my question is the second one i.e. *the active menu point must use the body class. Following is html snippet.
<body class="home">
<div id="wrapper">
<div id="nav">
<ul>
<li id="btnHome">Home</li>
<li id="btnAbout">About</li>
<li id="btnContact">Contact</li>
<li id="btnLinks">Links</li>
</ul>
</div>
</div>
</div>
How can I use body class to for menu items so when user is on home page then home link will be active using css. Same goes with the other links?
I think that means for every page the body class changes, so for the HomePage you have the class home, for the AboutPage you have the class about....
.home #btnHome {
/* active home menu code */
}
.about #btnAbout {
/* active about menu code */
}
Is that what you need?
I finally got Bootstrap tabs to work. I was wondering if there's a way to change the behaviour so instead of clicking just hovering the cursor would show the hidden content?
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs > li > a').hover(function() {
$(this).tab('show');
});
You wont have to modify the core bootstrap code.
Additionally you could do this:
$('.nav-tabs > li ').hover(function() {
if ($(this).hasClass('hoverblock'))
return;
else
$(this).find('a').tab('show');
});
$('.nav-tabs > li').find('a').click(function() {
$(this).parent()
.siblings().addClass('hoverblock');
});
Which will prevent hover selection after the first time a user clicks a tab.
It's better to bind a handler on the document object so it will work with dynamically generated tabs too:
$(document).on('mouseenter', '[data-toggle="tab"]', function () {
$(this).tab('show');
});
Also there is a small Bootstrap plugin which automatically activates tabs on hover: https://github.com/tonystar/bootstrap-hover-tabs.
The complete HTML using this plugin is:
body { padding: 2rem; }
.tab-content { margin-top: 1.5rem; }
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Nav pills -->
<ul class="nav nav-pills">
<li class="active">Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
</ul>
<!-- Tab panes -->
<div class="tab-content well">
<div class="tab-pane active" id="tab-1">Content 1</div>
<div class="tab-pane" id="tab-2">Content 2</div>
<div class="tab-pane" id="tab-3">Content 3</div>
<div class="tab-pane" id="tab-4">Content 4</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="//cdn.rawgit.com/tonystar/bootstrap-hover-tabs/v3.1.1/bootstrap-hover-tabs.js"></script>
JavaScript:
In your $(document).ready or elsewhere ...
put something like this:
$('.nav-tabs[data-toggle="tab-hover"] > li > a').hover( function(){
$(this).tab('show');
});
HTML:
<ul class="nav nav-tabs" data-toggle="tab-hover">
....
</ul>
Modify bootstrap.js (or boostrap-tab.js if you aren't using the full bootstrap.js file)
Where you see:
/* TAB DATA-API
* ============ */
$(function () {
$('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
e.preventDefault()
$(this).tab('show')
})
})
change the 'click.tab.data-api' to 'hover.tab.data-api'
Note: I tested this with Bootstrap v2.0.2
Right from my production code. Orthogonal, unobtrusive, can "unclick" any user interface. Selector can be modified to match many kinds of clickables.
$(document).on('mouseover','.js-mouseover-to-click',function (event) {
$(event.target).trigger('click');
});
i hope it is nice solution
(function ($) {
$(function () {
$(document).off('click.bs.tab.data-api', '[data-hover="tab"]');
$(document).on('mouseenter.bs.tab.data-api', '[data-toggle="tab"], [data-hover="tab"]', function () {
$(this).tab('show');
});
});
})(jQuery);