Guide_3DS/assets/js/plugins/jquery.greedy-navigation.js

93 lines
2 KiB
JavaScript
Raw Normal View History

2016-10-30 01:02:14 +02:00
/*
* Greedy Navigation
*
* http://codepen.io/lukejacksonn/pen/PwmwWV
*
*/
var $nav = $('#site-nav');
2017-01-15 07:06:59 +01:00
var $btn = $('#site-nav #toggle-nav');
2016-10-30 01:02:14 +02:00
var $vlinks = $('#site-nav .visible-links');
2017-01-15 07:06:59 +01:00
var $hlinks = $('#site-nav .hidden-links.links-menu');
2016-10-30 01:02:14 +02:00
var breaks = [];
function updateNav() {
2017-01-16 15:43:16 +01:00
var availableSpace = $btn.hasClass('hidden') ? $nav.width() : $nav.width() - $btn.width() - 80;
2016-10-30 01:02:14 +02:00
// The visible list is overflowing the nav
if($vlinks.width() > availableSpace) {
// Record the width of the list
breaks.push($vlinks.width());
// Move item to the hidden list
$vlinks.children().last().prependTo($hlinks);
// Show the dropdown btn
if($btn.hasClass('hidden')) {
$btn.removeClass('hidden');
}
2016-11-23 02:29:35 +01:00
2016-10-30 01:02:14 +02:00
// The visible list is not overflowing
} else {
// There is space for another item in the nav
if(availableSpace > breaks[breaks.length-1]) {
// Move the item to the visible list
$hlinks.children().first().appendTo($vlinks);
breaks.pop();
}
// Hide the dropdown btn if hidden list is empty
if(breaks.length < 1) {
$btn.addClass('hidden');
$hlinks.addClass('hidden');
}
}
// Keep counter updated
$btn.attr("count", breaks.length);
// Recur if the visible list is still overflowing the nav
if($vlinks.width() > availableSpace) {
updateNav();
}
}
// Window listeners
$(window).resize(function() {
updateNav();
});
$btn.on('click', function() {
2017-01-15 07:06:59 +01:00
if($hlinks.is(":visible")){
$hlinks.addClass('hidden');
$btn.removeClass('close');
} else {
$hlinks.removeClass('hidden');
$('.lang-menu').addClass('hidden');
$btn.addClass('close');
$('.lang-selector').removeClass('close');
}
});
// Language selector button
$('.lang-selector').on('click', function() {
if($('.lang-menu').is(":visible")){
$('.lang-menu').addClass('hidden');
$('.lang-selector').removeClass('close');
} else {
$('.lang-menu').removeClass('hidden');
$('.lang-selector').addClass('close');
$hlinks.addClass('hidden');
$btn.removeClass('close');
}
2016-10-30 01:02:14 +02:00
});
updateNav();