Skip to content

Commit

Permalink
Replace jQuery Smooth Scroll with Smooth Scroll + Gumshoe
Browse files Browse the repository at this point in the history
  • Loading branch information
mmistakes committed Mar 5, 2019
1 parent 10f62bb commit 20094a3
Show file tree
Hide file tree
Showing 8 changed files with 1,116 additions and 328 deletions.
93 changes: 32 additions & 61 deletions assets/js/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ $(document).ready(function() {
// Sticky footer
var bumpIt = function() {
$("body").css("margin-bottom", $(".page__footer").outerHeight(true));
};
},
didResize = false;

bumpIt();
$(window).resize(jQuery.throttle(250, function() {
bumpIt();
}));

$(window).resize(function() {
didResize = true;
});
setInterval(function() {
if (didResize) {
didResize = false;
bumpIt();
}
}, 250);

// FitVids init
$("#main").fitVids();
Expand Down Expand Up @@ -64,65 +72,28 @@ $(document).ready(function() {
});

// Smooth scrolling

// Bind popstate event listener to support back/forward buttons.
var smoothScrolling = false;
$(window).bind("popstate", function (event) {
$.smoothScroll({
scrollTarget: decodeURI(location.hash),
offset: -20,
beforeScroll: function() { smoothScrolling = true; },
afterScroll: function() { smoothScrolling = false; }
});
var scroll = new SmoothScroll('a[href*="#"]', {
offset: 20,
speed: 300
});
// Override clicking on links to smooth scroll
$('a[href*="#"]').bind("click", function (event) {
if (this.pathname === location.pathname && this.hash) {
event.preventDefault();
history.pushState(null, null, this.hash);
$(window).trigger("popstate");
}

// Gumshoe scroll spy init
var spy = new Gumshoe("nav.toc a", {
// Active classes
navClass: "active", // applied to the nav list item
contentClass: "active", // applied to the content

// Nested navigation
nested: false, // if true, add classes to parents of active link
nestedClass: "active", // applied to the parent items

// Offset & reflow
offset: 20, // how far from the top of the page to activate a content area
reflow: true, // if true, listen for reflows

// Event support
events: true // if true, emit custom events
});
// Smooth scroll on page load if there is a hash in the URL.
if (location.hash) {
$(window).trigger("popstate");
}

// Scrollspy equivalent: update hash fragment while scrolling.
$(window).scroll(jQuery.throttle(250, function() {
// Don't run while smooth scrolling (from clicking on a link).
if (smoothScrolling) return;
var scrollTop = $(window).scrollTop() + 20 + 1; // 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 !== decodeURI(location.hash)) {
history.replaceState(null, null, links[i].href);
}
return;
}
}
if ('#' !== location.hash) {
history.replaceState(null, null, '#');
}
}));

// add lightbox class to all image links
$(
Expand Down
8 changes: 4 additions & 4 deletions assets/js/main.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 20094a3

Please sign in to comment.