This script only makes the control when loading the page, How can I do this control during all browsing?

Asked

Viewed 39 times

3

This script controls the existence of the class hide-bar in id header page loading when the class is not yet added to it. I need to find a solution so that once the class is added to the id which is exactly when there is a scrolling of the page, the script is executed.

jQuery(function($){

    $( "#header", function() {
        //////////////////////////////////////////////
        if ( $("#header").hasClass("hide-bar") ){
             alert("A classe está adicionada.");
        }

    }); 

});

1 answer

3


You need to "listen" to the scroll/scroll event.

You can do it like this:

 $(window).on('scroll', function() {
    if ($("#header").hasClass("hide-bar")){
         alert("A classe está adicionada.");
    }
});

That way when the event happens it will call this code and do the check.

If you need to know the scroll position, you can use $(window).scrollTop(); that will give you the value of the scroll at the moment.

  • @Marcosvinicius great! If you want you can also use $(window).scrollTop() to know the position of the scroll.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.