vendredi 15 juillet 2016

Convert this jQuery to plain Javascript only [on hold]

I'd like to add a CSS class to a div without when the user scrolls without the need for loading the jQuery library (this is the only jQuery throughout the whole site so loading the whole library just for this is annoying). I have got it working in jQuery but would like to get it to work with just Javascript only. I have been looking for a few hours without any luck. Here is the code I have currently...

(function($) {
    var header = $(".site-header");
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 50) {
            header.addClass("shrink");
        } else {
            header.removeClass("shrink");
        }
    });
})(jQuery);

If anyone can help me convert this to plain javascript only that would be great!

Aucun commentaire:

Enregistrer un commentaire