mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
5ac0f751aa
723da4a37 Vendor the theme git-subtree-dir: docs git-subtree-split: 723da4a3769ee8a6c02758b553618143f92fccef
80 lines
2.3 KiB
JavaScript
80 lines
2.3 KiB
JavaScript
// query selector targets Hugo TOC
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
// Feature Test
|
|
if ('querySelector' in document && 'addEventListener' in window && Array.prototype.forEach) {
|
|
|
|
// Function to animate the scroll
|
|
var smoothScroll = function(anchor, duration) {
|
|
|
|
// Calculate how far and how fast to scroll
|
|
var startLocation = window.pageYOffset;
|
|
var endLocation = anchor.offsetTop;
|
|
var distance = endLocation - startLocation;
|
|
var increments = distance / (duration / 16);
|
|
var stopAnimation;
|
|
|
|
// Scroll the page by an increment, and check if it's time to stop
|
|
var animateScroll = function() {
|
|
window.scrollBy(0, increments);
|
|
stopAnimation();
|
|
};
|
|
|
|
// If scrolling down
|
|
if (increments >= 0) {
|
|
// Stop animation when you reach the anchor OR the bottom of the page
|
|
stopAnimation = function() {
|
|
var travelled = window.pageYOffset;
|
|
if ((travelled >= (endLocation - increments)) || ((window.innerHeight + travelled) >= document.body.offsetHeight)) {
|
|
clearInterval(runAnimation);
|
|
}
|
|
};
|
|
}
|
|
// If scrolling up
|
|
else {
|
|
// Stop animation when you reach the anchor OR the top of the page
|
|
stopAnimation = function() {
|
|
var travelled = window.pageYOffset;
|
|
if (travelled <= (endLocation || 0)) {
|
|
clearInterval(runAnimation);
|
|
}
|
|
};
|
|
}
|
|
|
|
// Loop the animation function
|
|
var runAnimation = setInterval(animateScroll, 16);
|
|
|
|
};
|
|
|
|
// Define smooth scroll links
|
|
var scrollToggle = document.querySelectorAll('#TableOfContents ul li a');
|
|
|
|
// For each smooth scroll link
|
|
[].forEach.call(scrollToggle, function(toggle) {
|
|
|
|
// When the smooth scroll link is clicked
|
|
toggle.addEventListener('click', function(e) {
|
|
|
|
// Prevent the default link behavior
|
|
e.preventDefault();
|
|
|
|
// Get anchor link and calculate distance from the top
|
|
var dataID = toggle.getAttribute('href');
|
|
var dataTarget = document.querySelector(dataID);
|
|
var dataSpeed = toggle.getAttribute('data-speed');
|
|
|
|
// If the anchor exists
|
|
if (dataTarget) {
|
|
// Scroll to the anchor
|
|
smoothScroll(dataTarget, dataSpeed || 500);
|
|
}
|
|
|
|
}, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})();
|