Update GreedyNav.js
This commit is contained in:
parent
d2ac1aebec
commit
3c93905e23
1 changed files with 58 additions and 56 deletions
|
@ -1,72 +1,74 @@
|
||||||
/*
|
/*
|
||||||
* Greedy Navigation
|
GreedyNav.js - https://github.com/lukejacksonn/GreedyNav
|
||||||
*
|
Licensed under the MIT license - http://opensource.org/licenses/MIT
|
||||||
* http://codepen.io/lukejacksonn/pen/PwmwWV
|
Copyright (c) 2015 Luke Jackson
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var $nav = $('#site-nav');
|
$(function() {
|
||||||
var $btn = $('#site-nav button');
|
|
||||||
var $vlinks = $('#site-nav .visible-links');
|
|
||||||
var $hlinks = $('#site-nav .hidden-links');
|
|
||||||
|
|
||||||
var breaks = [];
|
var $btn = $('nav.greedy-nav button');
|
||||||
|
var $vlinks = $('nav.greedy-nav .visible-links');
|
||||||
|
var $hlinks = $('nav.greedy-nav .hidden-links');
|
||||||
|
|
||||||
function updateNav() {
|
var numOfItems = 0;
|
||||||
|
var totalSpace = 0;
|
||||||
|
var closingTime = 1000;
|
||||||
|
var breakWidths = [];
|
||||||
|
|
||||||
var availableSpace = $btn.hasClass('hidden') ? $nav.width() : $nav.width() - $btn.width() - 30;
|
// Get initial state
|
||||||
|
$vlinks.children().outerWidth(function(i, w) {
|
||||||
|
totalSpace += w;
|
||||||
|
numOfItems += 1;
|
||||||
|
breakWidths.push(totalSpace);
|
||||||
|
});
|
||||||
|
|
||||||
// The visible list is overflowing the nav
|
var availableSpace, numOfVisibleItems, requiredSpace, timer;
|
||||||
if($vlinks.width() > availableSpace) {
|
|
||||||
|
|
||||||
// Record the width of the list
|
function check() {
|
||||||
breaks.push($vlinks.width());
|
|
||||||
|
|
||||||
// Move item to the hidden list
|
// Get instant state
|
||||||
$vlinks.children().last().prependTo($hlinks);
|
availableSpace = $vlinks.width() - 10;
|
||||||
|
numOfVisibleItems = $vlinks.children().length;
|
||||||
|
requiredSpace = breakWidths[numOfVisibleItems - 1];
|
||||||
|
|
||||||
// Show the dropdown btn
|
// There is not enough space
|
||||||
if($btn.hasClass('hidden')) {
|
if (requiredSpace > availableSpace) {
|
||||||
$btn.removeClass('hidden');
|
$vlinks.children().last().prependTo($hlinks);
|
||||||
}
|
numOfVisibleItems -= 1;
|
||||||
|
check();
|
||||||
// The visible list is not overflowing
|
// There is more than enough space
|
||||||
} else {
|
} else if (availableSpace > breakWidths[numOfVisibleItems]) {
|
||||||
|
|
||||||
// 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);
|
$hlinks.children().first().appendTo($vlinks);
|
||||||
breaks.pop();
|
numOfVisibleItems += 1;
|
||||||
|
check();
|
||||||
}
|
}
|
||||||
|
// Update the button accordingly
|
||||||
// Hide the dropdown btn if hidden list is empty
|
$btn.attr("count", numOfItems - numOfVisibleItems);
|
||||||
if(breaks.length < 1) {
|
if (numOfVisibleItems === numOfItems) {
|
||||||
$btn.addClass('hidden');
|
$btn.addClass('hidden');
|
||||||
|
} else $btn.removeClass('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Window listeners
|
||||||
|
$(window).resize(function() {
|
||||||
|
check();
|
||||||
|
});
|
||||||
|
|
||||||
|
$btn.on('click', function() {
|
||||||
|
$hlinks.toggleClass('hidden');
|
||||||
|
clearTimeout(timer);
|
||||||
|
});
|
||||||
|
|
||||||
|
$hlinks.on('mouseleave', function() {
|
||||||
|
// Mouse has left, start the timer
|
||||||
|
timer = setTimeout(function() {
|
||||||
$hlinks.addClass('hidden');
|
$hlinks.addClass('hidden');
|
||||||
}
|
}, closingTime);
|
||||||
}
|
}).on('mouseenter', function() {
|
||||||
|
// Mouse is back, cancel the timer
|
||||||
|
clearTimeout(timer);
|
||||||
|
})
|
||||||
|
|
||||||
// Keep counter updated
|
check();
|
||||||
$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() {
|
|
||||||
$hlinks.toggleClass('hidden');
|
|
||||||
$(this).toggleClass('close');
|
|
||||||
});
|
|
||||||
|
|
||||||
updateNav();
|
|
Loading…
Reference in a new issue