jquery-plugins: re-fix greedy-nav

Apparently I did in fact fix this in the 3DS guide long ago, but I
forgot to copy that here for some reason.
This commit is contained in:
lifehackerhansol 2023-09-28 19:57:50 -07:00
parent 4e7fce4b19
commit 5f26aad8e7
No known key found for this signature in database
GPG key ID: 80FB184AFC0B3B0E
2 changed files with 39 additions and 38 deletions

File diff suppressed because one or more lines are too long

View file

@ -6,15 +6,15 @@ Copyright (c) 2015 Luke Jackson
$(function() { $(function() {
var $btn1 = $('nav.greedy-nav .greedy-nav__toggle'); var $btn = $("nav.greedy-nav .greedy-nav__toggle");
var $btn2 = $('nav.greedy-nav .greedy-nav__toggle_lang'); var $btn2 = $("nav.greedy-nav .greedy-nav__toggle_lang");
var $vlinks = $("nav.greedy-nav .visible-links"); var $vlinks = $("nav.greedy-nav .visible-links");
var $hlinks1 = $('nav.greedy-nav .hidden-links.links-menu'); var $hlinks = $("nav.greedy-nav .hidden-links.links-menu");
var $hlinks2 = $('nav.greedy-nav .hidden-links.lang-menu'); var $hlinks2 = $("nav.greedy-nav .hidden-links.lang-menu");
var $nav = $("nav.greedy-nav"); var $nav = $("nav.greedy-nav");
var $logo = $('nav.greedy-nav .site-logo'); // var $logo = $('nav.greedy-nav .site-logo');
var $logoImg = $('nav.greedy-nav .site-logo img'); var $logoImg = $('nav.greedy-nav .site-logo img');
var $title = $("nav.greedy-nav .site-title"); // var $title = $("nav.greedy-nav .site-title");
var $search = $('nav.greedy-nav button.search__toggle'); var $search = $('nav.greedy-nav button.search__toggle');
var numOfItems, totalSpace, closingTime, breakWidths; var numOfItems, totalSpace, closingTime, breakWidths;
@ -45,7 +45,7 @@ $(function() {
} }
// Measure both visible and hidden links widths // Measure both visible and hidden links widths
$vlinks.children().outerWidth(addWidth); $vlinks.children().outerWidth(addWidth);
$hlinks1.children().each(function(){hiddenWidth($(this))}); $hlinks.children().each(function(){hiddenWidth($(this))});
} }
// Get initial state // Get initial state
measureLinks(); measureLinks();
@ -54,7 +54,7 @@ $(function() {
// Set the last measured CSS width breakpoint: 0: <768px, 1: <1024px, 2: < 1280px, 3: >= 1280px. // Set the last measured CSS width breakpoint: 0: <768px, 1: <1024px, 2: < 1280px, 3: >= 1280px.
var lastBreakpoint = winWidth < 768 ? 0 : winWidth < 1024 ? 1 : winWidth < 1280 ? 2 : 3; var lastBreakpoint = winWidth < 768 ? 0 : winWidth < 1024 ? 1 : winWidth < 1280 ? 2 : 3;
var availableSpace, numOfVisibleItems, requiredSpace, timer1, timer2; var availableSpace, numOfVisibleItems, requiredSpace, timer, timer2;
function check() { function check() {
@ -70,28 +70,29 @@ $(function() {
numOfVisibleItems = $vlinks.children().length; numOfVisibleItems = $vlinks.children().length;
// Decrease the width of visible elements from the nav innerWidth to find out the available space for navItems // Decrease the width of visible elements from the nav innerWidth to find out the available space for navItems
availableSpace = /* nav */ $nav.innerWidth() availableSpace = /* nav */ $nav.innerWidth()
- /* logo */ ($logo.length !== 0 ? $logo.outerWidth(true) : 0) - /* logo */ // ($logo.length !== 0 ? $logo.outerWidth(true) : 0)
- /* title */ $title.outerWidth(true) - /* title */ // $title.outerWidth(true)
- /* search */ ($search.length !== 0 ? $search.outerWidth(true) : 0) - /* search */ ($search.length !== 0 ? $search.outerWidth(true) : 0)
- /* toggle */ (numOfVisibleItems !== breakWidths.length ? $btn1.outerWidth(true) : 0); - /* toggle */ (numOfVisibleItems !== breakWidths.length ? $btn.outerWidth(true) : 0)
- /* toggle-lang */ ($btn2.outerWidth(true));
requiredSpace = breakWidths[numOfVisibleItems - 1]; requiredSpace = breakWidths[numOfVisibleItems - 1];
// There is not enought space // There is not enought space
if (requiredSpace > availableSpace) { if (requiredSpace > availableSpace) {
$vlinks.children().last().prependTo($hlinks1); $vlinks.children().last().prependTo($hlinks);
numOfVisibleItems -= 1; numOfVisibleItems -= 1;
check(); check();
// There is more than enough space. If only one element is hidden, add the toggle width to the available space // There is more than enough space. If only one element is hidden, add the toggle width to the available space
} else if (availableSpace + (numOfVisibleItems === breakWidths.length - 1?$btn1.outerWidth(true):0) > breakWidths[numOfVisibleItems]) { } else if (availableSpace + (numOfVisibleItems === breakWidths.length - 1?$btn.outerWidth(true):0) > breakWidths[numOfVisibleItems]) {
$hlinks1.children().first().appendTo($vlinks); $hlinks.children().first().appendTo($vlinks);
numOfVisibleItems += 1; numOfVisibleItems += 1;
check(); check();
} }
// Update the button accordingly // Update the button accordingly
$btn1.attr("count", numOfItems - numOfVisibleItems); $btn.attr("count", numOfItems - numOfVisibleItems);
if (numOfVisibleItems === numOfItems) { if (numOfVisibleItems === numOfItems) {
$btn1.addClass('hidden'); $btn.addClass('hidden');
} else $btn1.removeClass('hidden'); } else $btn.removeClass('hidden');
} }
// Window listeners // Window listeners
@ -99,53 +100,53 @@ $(function() {
check(); check();
}); });
$btn1.on('click', function() { $btn.on('click', function() {
if($hlinks1.is(":visible")){ if($hlinks.is(":visible")){
$hlinks1.addClass('hidden'); $hlinks.addClass('hidden');
$(this).removeClass('close'); $(this).removeClass('close');
clearTimeout(timer);
} else { } else {
$hlinks1.removeClass('hidden'); $hlinks.removeClass('hidden');
$(this).addClass('close'); $(this).addClass('close');
$hlinks2.addClass('hidden'); $hlinks2.addClass('hidden');
$btn2.removeClass('close'); $btn2.removeClass('close');
clearTimeout(timer2); clearTimeout(timer);
} }
}); });
$hlinks1.on('mouseleave', function() { $hlinks.on('mouseleave', function() {
// Mouse has left, start the timer1 // Mouse has left, start the timer
timer1 = setTimeout(function() { timer = setTimeout(function() {
$hlinks1.addClass('hidden'); $hlinks.addClass('hidden');
$btn1.removeClass('close');
}, closingTime); }, closingTime);
}).on('mouseenter', function() { }).on('mouseenter', function() {
// Mouse is back, cancel the timer1 // Mouse is back, cancel the timer
clearTimeout(timer1); clearTimeout(timer);
}); })
$btn2.on('click', function() { $btn2.on('click', function() {
if($hlinks2.is(":visible")){ if($hlinks2.is(":visible")){
$hlinks2.addClass('hidden'); $hlinks2.addClass('hidden');
$(this).removeClass('close'); $(this).removeClass('close');
clearTimeout(timer2);
} else { } else {
$hlinks2.removeClass('hidden'); $hlinks2.removeClass('hidden');
$(this).addClass('close'); $(this).addClass('close');
$hlinks1.addClass('hidden'); $hlinks.addClass('hidden');
$btn1.removeClass('close'); $btn.removeClass('close');
clearTimeout(timer1); clearTimeout(timer2);
} }
}); });
$hlinks2.on('mouseleave', function() { $hlinks2.on('mouseleave', function() {
// Mouse has left, start the timer2 // Mouse has left, start the timer
timer2 = setTimeout(function() { timer2 = setTimeout(function() {
$hlinks2.addClass('hidden'); $hlinks2.addClass('hidden');
$btn2.removeClass('close');
}, closingTime); }, closingTime);
}).on('mouseenter', function() { }).on('mouseenter', function() {
// Mouse is back, cancel the timer2 // Mouse is back, cancel the timer
clearTimeout(timer2); clearTimeout(timer2);
}); })
// check if page has a logo // check if page has a logo
if($logoImg.length !== 0){ if($logoImg.length !== 0){