Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git# This is a combination of 2 commits.
Refactoring footer-button and removing jQuery
- Loading branch information
Steven Bellamy
committed
May 20, 2016
1 parent
cf503ce
commit 3a3b643
Showing
3 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
/* ========================================================================== | ||
Footer Button: Scroll to Top | ||
Code copied from the following with minimal modifications : | ||
- http://stackoverflow.com/questions/21474678/ | ||
scrolltop-animation-without-jquery | ||
========================================================================== */ | ||
|
||
'use strict'; | ||
|
||
var $ = require( 'jquery' ); | ||
// Required modules. | ||
var dataHook = require( './util/data-hook' ); | ||
|
||
// TODO: Refactor this file to remove jquery dependency. | ||
// http://stackoverflow.com/questions/21474678/ | ||
// scrolltop-animation-without-jquery | ||
/** | ||
* Set up event handler for button to scroll to top of page. | ||
*/ | ||
function init() { | ||
var duration = 300; | ||
var scrollDuration = 300; | ||
var behaviorElement = dataHook.get( 'return-to-top' ); | ||
|
||
// Disable Google Tag Manager tracking on this link. | ||
$( '.js-return-to-top' ).attr( 'data-gtm_ignore', 'true' ); | ||
if ( behaviorElement === null || | ||
'requestAnimationFrame' in window === false ) { | ||
return; | ||
} | ||
|
||
$( '.js-return-to-top' ).click( function( event ) { | ||
// Disable Google Tag Manager tracking on this link. | ||
behaviorElement.setAttribute( 'data-gtm_ignore', 'true' ); | ||
behaviorElement.addEventListener( 'click', function( event ) { | ||
event.preventDefault(); | ||
$( 'html, body' ).animate( { scrollTop: 0 }, duration ); | ||
_scrollToTop( scrollDuration ); | ||
} ); | ||
} | ||
|
||
/** | ||
* @param {integer} scrollDuration | ||
* Duration of the scroll to top of the page. | ||
*/ | ||
function _scrollToTop( scrollDuration ) { | ||
var scrollHeight = window.scrollY; | ||
var scrollStep = Math.PI / ( scrollDuration / 15 ); | ||
var cosParameter = scrollHeight / 2; | ||
var scrollCount = 0; | ||
var scrollMargin; | ||
requestAnimationFrame( _step ); | ||
|
||
/** | ||
* Decrement scroll Y position. | ||
*/ | ||
function _step() { | ||
setTimeout( function() { | ||
if ( window.scrollY !== 0 ) { | ||
requestAnimationFrame( _step ); | ||
scrollCount += 1; | ||
scrollMargin = cosParameter - cosParameter * | ||
Math.cos( scrollCount * scrollStep ); | ||
window.scrollTo( 0, scrollHeight - scrollMargin ); | ||
} | ||
}, 15 ); | ||
} | ||
|
||
} | ||
|
||
module.exports = { init: init }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters