Skip to content

Instantly share code, notes, and snippets.

@kad3nce
Last active November 12, 2015 18:53
Show Gist options
  • Save kad3nce/2d0db8e8e43cc4de097d to your computer and use it in GitHub Desktop.
Save kad3nce/2d0db8e8e43cc4de097d to your computer and use it in GitHub Desktop.
Automatic Page Scroller
var scrollElement = function (element, scrollDistance, scrollDuration) {
var style = element.style;
// setup CSS transition duration and easing function
style.webkitTransition =
style.transition = scrollDuration + 's';
style.webkitTransitionTimingFunction =
style.TransitionTimingFunction = 'ease-in-out';
// use translate3d to force hardware acceleration
style.webkitTransform =
style.Transform = 'translate3d(0, ' + -scrollDistance + 'px, 0)';
};
var delayFuncCall = function (/*duration, func, funcArgs...*/) {
var args = [].slice.call(arguments);
var duration = args.shift(),
func = args.shift(),
funcArgs = args;
setTimeout(function () {
func.apply(null, funcArgs);
}, duration * 1000);
};
var promptForArgs = function (prependString) {
var description = "Enter delay (in seconds), distance to scroll (in pixels) and the scroll duration (in seconds). Example:\n\n5 800 3";
if (prependString) {
description = prependString + "\n\n" + description;
}
var input = prompt(description);
// user clicked 'Cancel'
if (input === null) {
return;
}
// user input was valid
if (/\d{1,} \d{1,} \d{1,}/.test(input)) {
return input.split(' ');
}
// user input was invalid
return promptForArgs('Woops. "' + input + '" is not valid input.');
};
var scrollViaPrompt = function () {
var args = promptForArgs();
var body = document.getElementsByTagName('body')[0],
delay = args[0],
scrollDistance = args[1],
scrollDuration = args[2];
if (args) {
delayFuncCall(delay, scrollElement, body, scrollDistance, scrollDuration);
}
};
scrollViaPrompt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment