Created
September 13, 2011 21:05
-
-
Save mattwiebe/1215166 to your computer and use it in GitHub Desktop.
Do something after you scroll past a certain element. Maybe do something else when you scroll back above it.
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
/*! | |
* jQuery AfterScroll 1.0 | |
* | |
* Do something once the bottom of an element comes into view, | |
* and something when you scroll above it too. | |
* | |
* (c) 2011 Matt Wiebe http://somadesign.ca/ | |
* GPL2 / MIT dual-license, just like jQuery | |
* http://jquery.org/license/ | |
* | |
*/ | |
(function($){ | |
$.fn.afterScroll = function(after, before) { | |
var $win = $(window) | |
after = after || $.noop | |
before = before || $.noop | |
return this.each(function() { | |
var t = this, | |
self = $(t), | |
elOffset = self.offset(), | |
elBottomPos = self.outerHeight() + elOffset.top, | |
scrolled = false | |
$win.scroll(function() { | |
// haven't scrolled past yet | |
if ( ! scrolled && $win.scrollTop() + $win.height() >= elBottomPos ) { | |
after.apply(t) | |
scrolled = true | |
} | |
// have scrolled past yet | |
else if ( scrolled && $win.scrollTop() + $win.height() < elBottomPos ) { | |
before.apply(t) | |
scrolled = false | |
} | |
}).scroll() | |
}) | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very nice to use!
I noticed the license is GPL2/MIT (just like jQuery)
jQuery has since changed their license to be only MIT.
Is this also the case with this plugin?