Skip to content

Instantly share code, notes, and snippets.

@mattwiebe
Created September 13, 2011 21:05
Show Gist options
  • Save mattwiebe/1215166 to your computer and use it in GitHub Desktop.
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.
/*!
* 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);
@nickolasjadams
Copy link

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?

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