Skip to content

Commit

Permalink
Prevent JS error if back to top HTML doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusborne committed May 7, 2018
1 parent eb16c96 commit cbe057f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
72 changes: 38 additions & 34 deletions js/back-to-top.js
@@ -1,8 +1,10 @@
( function() {
'use strict';
'use strict';

// Feature Test
if ( 'querySelector' in document && 'addEventListener' in window ) {
if ( 'querySelector' in document && 'addEventListener' in window ) {

var goTopBtn = document.querySelector( '.generate-back-to-top' );

var trackScroll = function() {
var scrolled = window.pageYOffset;
Expand All @@ -17,44 +19,46 @@
goTopBtn.style.opacity = '0';
goTopBtn.style.visibility = 'hidden';
}
}
window.addEventListener( 'scroll', trackScroll );
};

// Function to animate the scroll
var smoothScroll = function (anchor, duration) {
// Function to animate the scroll
var smoothScroll = function (anchor, duration) {
// Calculate how far and how fast to scroll
var startLocation = window.pageYOffset;
var endLocation = document.body.offsetTop;
var distance = endLocation - startLocation;
var increments = distance/(duration/16);
var stopAnimation;

// Calculate how far and how fast to scroll
var startLocation = window.pageYOffset;
var endLocation = document.body.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();
};

// Scroll the page by an increment, and check if it's time to stop
var animateScroll = function () {
window.scrollBy(0, increments);
stopAnimation();
};
// 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);
}
};

// 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);
};

// Loop the animation function
var runAnimation = setInterval(animateScroll, 16);
if ( goTopBtn ) {
// Show the button when scrolling down.
window.addEventListener( 'scroll', trackScroll );

};

var goTopBtn = document.querySelector( '.generate-back-to-top' );
goTopBtn.addEventListener( 'click', function( e ) {
e.preventDefault();
smoothScroll( document.body, goTopBtn.getAttribute( 'data-scroll-speed' ) || 400 );
}, false );
// Scroll back to top when clicked.
goTopBtn.addEventListener( 'click', function( e ) {
e.preventDefault();
smoothScroll( document.body, goTopBtn.getAttribute( 'data-scroll-speed' ) || 400 );
}, false );
}

}
}

} )();
2 changes: 1 addition & 1 deletion js/back-to-top.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cbe057f

Please sign in to comment.