Update GreedyNav.js

This commit is contained in:
Michael Rose
2020-06-06 10:30:54 -04:00
parent 3ab6ba0b1f
commit 7ec8e90c76
4 changed files with 32 additions and 24 deletions

View File

@ -1,16 +1,18 @@
/*
GreedyNav.js - https://github.com/lukejacksonn/GreedyNav
GreedyNav.js - http://lukejacksonn.com/actuate
Licensed under the MIT license - http://opensource.org/licenses/MIT
Copyright (c) 2015 Luke Jackson
*/
$(document).ready(function() {
$(function() {
var $btn = $("nav.greedy-nav .greedy-nav__toggle");
var $vlinks = $("nav.greedy-nav .visible-links");
var $hlinks = $("nav.greedy-nav .hidden-links");
var numOfItems = 0;
var totalSpace = 0;
var closingTime = 1000;
var breakWidths = [];
// Get initial state
@ -20,38 +22,31 @@ $(document).ready(function() {
breakWidths.push(totalSpace);
});
var availableSpace, numOfVisibleItems, requiredSpace;
var availableSpace, numOfVisibleItems, requiredSpace, timer;
function check() {
// Get instant state
availableSpace = $vlinks.width() - $btn.width();
availableSpace = $vlinks.width() - 10;
numOfVisibleItems = $vlinks.children().length;
requiredSpace = breakWidths[numOfVisibleItems - 1];
// There is not enough space
// There is not enought space
if (requiredSpace > availableSpace) {
$vlinks
.children()
.last()
.prependTo($hlinks);
$vlinks.children().last().prependTo($hlinks);
numOfVisibleItems -= 1;
check();
// There is more than enough space
} else if (availableSpace > breakWidths[numOfVisibleItems]) {
$hlinks
.children()
.first()
.appendTo($vlinks);
$hlinks.children().first().appendTo($vlinks);
numOfVisibleItems += 1;
check();
}
// Update the button accordingly
$btn.attr("count", numOfItems - numOfVisibleItems);
if (numOfVisibleItems === numOfItems) {
$btn.addClass("hidden");
} else {
$btn.removeClass("hidden");
}
$btn.addClass('hidden');
} else $btn.removeClass('hidden');
}
// Window listeners
@ -59,10 +54,21 @@ $(document).ready(function() {
check();
});
$btn.on("click", function() {
$hlinks.toggleClass("hidden");
$(this).toggleClass("close");
$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');
}, closingTime);
}).on('mouseenter', function() {
// Mouse is back, cancel the timer
clearTimeout(timer);
})
check();
});
});