Use jquery to control scrolltop

Asked

Viewed 156 times

0

I have a div .menuFixo, that when it reaches 31px scroll she stay with top: 0, otherwise, stay top:31px, I did with Jquery, but it didn’t work.

$(window).scroll(function () {
    if ($(window).scrollTop() > 31) {
        $(".menuFixo").css("top", "0");
    };
    else if ($(window).scrollTop() < 31) {
        $(".menuFixo").css("top", "31px");
    };

});
  • I don’t think you need the second if, only else

  • I tried with only else and it didn’t work

  • @Felipe Stoker Try to put one Alert to see if this calling function.

  • I couldn’t figure out what it was, finally I did what I wanted otherwise.

1 answer

1


Look if it can help you.

        $(document).ready(function () {
          $(window).scroll(function () {
            if ($(this).scrollTop() > 31) {
                $(".menuFixo").css("top", "0");
          }else {
                $(".menuFixo").css("top", "31px");
          }
                });
        });
  • I discovered my mistake, I was ending with ; before starting the else.

Browser other questions tagged

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