Skip to content

Instantly share code, notes, and snippets.

@ericktatsui
Forked from james2doyle/scrollTo.js
Last active February 20, 2016 16:45
Show Gist options
  • Save ericktatsui/2fec7cad187198ba0de6 to your computer and use it in GitHub Desktop.
Save ericktatsui/2fec7cad187198ba0de6 to your computer and use it in GitHub Desktop.
a native scrollTo function in javascript that uses requestAnimationFrame and easing for animation
var scrollTo = function (opt) {
var self,
cache = {};
var scrollTo = function () {
self = this;
this.start = self.getCurrentPosition();
this.change = self.getNewPosition() - this.start;
this.currentTime = 0;
this.increment = 20;
this.duration = (typeof (opt.duration) === 'undefined') ? 500 : duration;
this.element = (typeof (opt.element) === 'undefined' ? ((navigator.userAgent.indexOf('Firefox') != -1 || navigator.userAgent.indexOf('MSIE') != -1) ? document.documentElement : document.body) : opt.element);
if (!this.fail) {
this.animate();
}
};
scrollTo.prototype.animate = function () {
// increment the time
self.currentTime += self.increment;
// find the value with the quadratic in-out easing function
var val = self.easing()(self.currentTime, self.start, self.change, self.duration);
// move the document.body
self.element.scrollTop = val;
// do the animation unless its over
if (self.currentTime < self.duration) {
self.requestAnimFrame()(self.animate);
} else {
if (opt.callback && typeof (opt.callback) === 'function') {
// the animation is done so lets callback
opt.callback();
}
}
};
scrollTo.prototype.callFail = function () {
self.fail = true;
console.error('Argument is not valid.');
};
scrollTo.prototype.getNewPosition = function () {
var position = 0,
element;
if (typeof (opt.targetName) == 'string') {
element = document.querySelector(opt.targetName);
if (element) {
position = (element.getBoundingClientRect()).top + window.scrollY;
} else {
self.callFail();
}
} else if (typeof (opt.position) == 'number') {
position = opt.position;
} else if (typeof (opt.target) == 'object' && 'getBoundingClientRect' in opt.target) {
position = (opt.target.getBoundingClientRect()).top + window.scrollY;
} else {
self.callFail();
}
return position;
};
scrollTo.prototype.easing = function () {
var easeType;
if(!cache.easing){
switch (opt.easing) {
case 'easeInCubic':
easeType = self.easeInCubic;
break;
case 'inOutQuintic':
easeType = self.inOutQuintic;
break;
default:
easeType = self.easeInOutQuad;
break;
}
cache.easing = easeType;
}else{
easeType = cache.easing;
}
return easeType;
};
scrollTo.prototype.easeInOutQuad = function (t, b, c, d) {
t /= d / 2;
if (t < 1) {
return c / 2 * t * t + b
}
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
};
scrollTo.prototype.easeInCubic = function (t, b, c, d) {
var tc = (t /= d) * t * t;
return b + c * (tc);
};
scrollTo.prototype.inOutQuintic = function (t, b, c, d) {
var ts = (t /= d) * t,
tc = ts * t;
return b + c * (6 * tc * ts + -15 * ts * ts + 10 * tc);
};
// polyfill
scrollTo.prototype.requestAnimFrame = function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); };
};
scrollTo.prototype.getCurrentPosition = function () {
return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop;
};
return new scrollTo();
};
@ericktatsui
Copy link
Author

changed the structure and add option to go to an element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment