I have a vertical 2 level jQuery menu, and I'd need that after a link has been clicked and the new page loaded, the menu stay open with the selected link highlighted.
Here is my menu code:
<div id="my-menu">
<ul>
<li><span class="toggle">Una Passione</span>
<ul>
<li>Tre Fratelli</li>
<li>Con le nostre mani</li>
<li>La nostra storia</li>
<li>Il video</li>
<li>Il libro</li>
</ul>
</li>
<li><span class="toggle">Icone</span>
<ul>
<li>Brera</li>
<li>Magenta</li>
</ul>
</li>
</ul>
</div>
and here the jQuery code:
$(document).ready(function() {
$("span.toggle").next().hide();
$("span.toggleinterno").next().hide();
$("#my-menu a, #my-menu span.toggle").click(function() {
$(this).stop().animate( {
color:"red"
}, 300);
});
$("span.toggle").click(function() {
if ($(this).hasClass('second')) {
$(this).removeClass('second');
} else {
$(this).addClass('second');
}
});
$("#my-menu a, #my-menu span.toggleinterno").click(function() {
$(this).stop().animate( {
fontSize:"17px",
//paddingLeft:"10px",
color:"black"
}, 300);
}, function() {
$(this).stop().animate( {
//fontSize:"14px",
paddingLeft:"15",
color:"#808080"
}, 300);
});
$("span.toggle").css("cursor", "pointer");
$("span.toggleinterno").css("cursor", "pointer");
$("span.toggle").click(function() {
$(this).next().toggle(1000);
});
$("span.toggleinterno").click(function() {
$("span.toggleinterno").css("width", "100px");
$(this).next().toggle(1000);
});
});
You could try something like this:
var url = window.location.pathname.match(/.*\/(.*)$/)[1];
var $activelink = $('#my-menu a[href="' + url + '"]');
var $openmenu = $('#my-menu ul ul').has(activelink);
$activelink.addClass('hilight');
$openmenu.show();
Related
I try do accessibility menu. I use jquery .focus, but there is no reaction. No classes are added.
My code:
<script type="text/javascript">
(function($) {
$('.menu-item-has-children a').focus( function () {
$(this).siblings('.sub-menu').addClass('focused');
}).blur(function(){
$(this).siblings('.sub-menu').removeClass('focused');
});
$('.sub-menu a').focus( function () {
$(this).parents('.sub-menu').addClass('focused');
}).blur(function(){
$(this).parents('.sub-menu').removeClass('focused');
});
})(jQuery);
</script>
Classes which I using are correct. There are no bugs in the console
Hi Check this working fine for me may be you have any other issue.
(function($) {
$('.menu-item-has-children a').focus( function () {
$(this).siblings('.sub-menu').addClass('focused');
}).blur(function(){
$(this).siblings('.sub-menu').removeClass('focused');
});
$('.sub-menu a').focus( function () {
$(this).parents('.sub-menu').addClass('focused');
}).blur(function(){
$(this).parents('.sub-menu').removeClass('focused');
});
})(jQuery);
.sub-menu{
opacity:0;
}
.sub-menu.focused{
opacity:1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<ul>
<li class="menu-item-has-children">
Link 1
<div class="sub-menu">
sub menu working
</div>
</li>
</ul>
</div>
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'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);
});
});
}
});
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;
}
Currently, I am using something like
<li class="active">Home</li>
<li>A</li>
<li>B</li>
<li>C</li>
where I have four html files index.html, a.html, b.html and c.html, which are selected based on the links clicked.
Instead, I want to have the content all in the same HTML file with common headers and footers and just selectively display content depending on which button was clicked.
How can I do that?
you can do this
put content of each page in div has unique id and display all none and each a in li have id of div
html
<li><a class="div1" href="#1">A</a></li>
<li><a class="div2" href="#2">B</a></li>
<li><a class="div3" href="#3">C</a></li>
<div id="1" class="hide" style=" width:100%; height: 100px; background-color:red; "></div>
<div id="2" class="hide" style=" width:100%; height: 100px; background-color:gold; "></div>
CSS
.hide
{
display:none;
}
JS
<script>
$(function () {
$('li').on('click', function (e) {
var href = $(this).find('a').attr('href');
window.location.hash = href;
});
$('.div1').on('click', function (e) {
$("#1").removeClass("hide");
$("#2").addClass("hide");
$("#3").addClass("hide");
});
$('.div2').on('click', function (e) {
$("#2").removeClass("hide");
$("#1").addClass("hide");
$("#3").addClass("hide");
});
$('.div3').on('click', function (e) {
$("#3").removeClass("hide");
$("#1").addClass("hide");
$("#2").addClass("hide");
});
if (window.location.hash == "#1") {
$("#1").removeClass("hide");
$("#3").addClass("hide");
$("#2").addClass("hide");
}
if (window.location.hash == "#2") {
$("#2").removeClass("hide");
$("#3").addClass("hide");
$("#1").addClass("hide");
}
if (window.location.hash == "#3") {
$("#3").removeClass("hide");
$("#1").addClass("hide");
$("#2").addClass("hide");
}
});
</script>