Use jQuery throttle library

This commit is contained in:
Erik Demaine
2019-01-04 12:53:36 -05:00
parent f2f580101e
commit 6990f8fcc9
5 changed files with 294 additions and 50 deletions

View File

@@ -6,20 +6,12 @@ $(document).ready(function() {
// Sticky footer
var bumpIt = function() {
$("body").css("margin-bottom", $(".page__footer").outerHeight(true));
},
didResize = false;
};
bumpIt();
$(window).resize(function() {
didResize = true;
});
setInterval(function() {
if (didResize) {
didResize = false;
bumpIt();
}
}, 250);
$(window).resize(jQuery.throttle(250, function() {
bumpIt();
}));
// FitVids init
$("#main").fitVids();
@@ -87,42 +79,37 @@ $(document).ready(function() {
}
// Scrollspy equivalent: update hash fragment while scrolling.
var timer;
$(window).scroll(function() {
$(window).scroll(jQuery.throttle(250, function() {
// Don't run while smooth scrolling (from clicking on a link).
if (smoothScrolling) return;
// Debounce: only run after 250 ms of no scrolling.
clearTimeout(timer);
timer = setTimeout(function () {
var scrollTop = $(window).scrollTop() + 20; // 20 = offset
var links = [];
$("nav.toc a").each(function() {
var link = $(this);
var href = link.attr("href");
if (href && href[0] == "#") {
var element = $(href);
links.push({
link: link,
href: href,
top: element.offset().top
});
link.removeClass('active');
}
});
for (var i = 0; i < links.length; i++) {
var top = links[i].top;
var bottom = (i < links.length - 1 ? links[i+1].top : Infinity);
if (top <= scrollTop && scrollTop < bottom) {
// Mark all ancestors as active
links[i].link.parents("li").children("a").addClass('active');
if (links[i].href !== location.hash) {
history.replaceState(null, null, links[i].href);
}
break;
}
var scrollTop = $(window).scrollTop() + 20; // 20 = offset
var links = [];
$("nav.toc a").each(function() {
var link = $(this);
var href = link.attr("href");
if (href && href[0] == "#") {
var element = $(href);
links.push({
link: link,
href: href,
top: element.offset().top
});
link.removeClass('active');
}
}, 250);
});
});
for (var i = 0; i < links.length; i++) {
var top = links[i].top;
var bottom = (i < links.length - 1 ? links[i+1].top : Infinity);
if (top <= scrollTop && scrollTop < bottom) {
// Mark all ancestors as active
links[i].link.parents("li").children("a").addClass('active');
if (links[i].href !== location.hash) {
history.replaceState(null, null, links[i].href);
}
break;
}
}
}));
// add lightbox class to all image links
$(