Created
August 9, 2016 01:04
-
-
Save asvny/c58da5c01192878da7ec486ad9b5b6b6 to your computer and use it in GitHub Desktop.
scroll native js
This file contains hidden or 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
function scrollIt(element, duration = 200, easing = 'linear', callback) { | |
// define timing functions | |
const easings = { | |
linear(t) { | |
return t; | |
}, | |
easeInQuad(t) { | |
return t * t; | |
}, | |
easeOutQuad(t) { | |
return t * (2 - t); | |
}, | |
easeInOutQuad(t) { | |
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; | |
}, | |
easeInCubic(t) { | |
return t * t * t; | |
}, | |
easeOutCubic(t) { | |
return (--t) * t * t + 1; | |
}, | |
easeInOutCubic(t) { | |
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1; | |
}, | |
easeInQuart(t) { | |
return t * t * t * t; | |
}, | |
easeOutQuart(t) { | |
return 1 - (--t) * t * t * t; | |
}, | |
easeInOutQuart(t) { | |
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t; | |
}, | |
easeInQuint(t) { | |
return t * t * t * t * t; | |
}, | |
easeOutQuint(t) { | |
return 1 + (--t) * t * t * t * t; | |
}, | |
easeInOutQuint(t) { | |
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t; | |
} | |
}; | |
// Returns document.documentElement for Chrome and Safari | |
// document.body for rest of the world | |
function checkBody() { | |
document.documentElement.scrollTop += 1; | |
const body = (document.documentElement.scrollTop !== 0) ? document.documentElement : document.body; | |
document.documentElement.scrollTop -= 1; | |
return body; | |
} | |
const body = checkBody(); | |
const start = body.scrollTop; | |
const startTime = Date.now(); | |
// Height checks to prevent requestAnimationFrame from infinitely looping | |
// If the function tries to scroll below the visible document area | |
// it should only scroll to the bottom of the document | |
const documentHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight); | |
const windowHeight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight; | |
const destination = documentHeight - element.offsetTop < windowHeight ? documentHeight - windowHeight : element.offsetTop; | |
function scroll() { | |
const now = Date.now(); | |
const time = Math.min(1, ((now - startTime) / duration)); | |
const timeFunction = easings[easing](time); | |
body.scrollTop = (timeFunction * (destination - start)) + start; | |
if (body.scrollTop === destination) { | |
callback(); | |
return; | |
} | |
requestAnimationFrame(scroll); | |
} | |
scroll(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment