Skip to content

Instantly share code, notes, and snippets.

@mindfullsilence
Last active December 22, 2015 22:41
Show Gist options
  • Save mindfullsilence/cfcc9d99c8c6dd4357b0 to your computer and use it in GitHub Desktop.
Save mindfullsilence/cfcc9d99c8c6dd4357b0 to your computer and use it in GitHub Desktop.
Scrolling animation wrapper
jQuery ScrollIt Plugin
{
"name": "jquery.scrollit.js",
"main": "jquery.scrollit.js",
"licence": "MIT",
"private": "true"
}
/**
* Animated scroll of a page. Includes an interrupter that stops the animation
* if the user scrolls.
*a
* options = {
* extraOffset : 0 amount of padding between top of element and window
* duration : 500 Animation duration
* delay : 0 Wait before scrolling
* window : $('html,body') Box to be scrolled (can set on div with overflow:scroll)
* }
*/
(function($) {
jQuery.fn.scrollIt = function( options ) {
var t = this,
isScrolling = false,
queue = 'scroller',
settings = $.extend({
extraOffset : 0,
duration : 500,
delay : 0,
window : $('html, body')
}, options);
function init() {
isScrolling = true;
beginScrolling();
setInterrupt();
return t;
}
function beginScrolling() {
settings.window.delay(settings.delay).animate({
scrollTop : t.offset().top + settings.extraOffset
},{
duration : settings.duration,
queue : queue
});
settings.window.dequeue(queue);
}
function setInterrupt() {
settings.window.on('touchstart mousewheel DOMMouseScroll', function() {
if(isScrolling) {
settings.window.stop('scroller', true, false).clearQueue(queue);
isScrolling = false;
}
});
}
return init();
};
}(jQuery));
@mindfullsilence
Copy link
Author

Use:

$('a').click(function() {
    $('#some-header').scrollIt({
        extraOffset : 50,
        duration : 1000,
        delay : 200,
        window : $('div#some-box')
    });
});

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