Last active
September 15, 2018 14:38
-
-
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.
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
(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