Skip to content

Instantly share code, notes, and snippets.

@timstl
Last active September 15, 2018 14:38
Show Gist options
  • Save timstl/2e1b9bb9f604fb195c8a to your computer and use it in GitHub Desktop.
Save timstl/2e1b9bb9f604fb195c8a to your computer and use it in GitHub Desktop.
Toggle class based on scroll position. Add your effects and style changes to your CSS.
(function($) {
$.fn.scrollclass = function(options) {
var opts = $.extend({}, $.fn.scrollclass.defaults, options),
$this = this,
state2 = false;
$(document)
.scroll(function() {
if (state2 == false && $(this).scrollTop() > opts.pos) {
state2 = true;
$this.addClass(opts.class);
} else if ($(this).scrollTop() < opts.pos) {
state2 = false;
$this.removeClass(opts.class);
}
})
.trigger("scroll");
};
// default options
$.fn.scrollclass.defaults = {
class: "scrolled",
pos: 50
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment