How to perform an action when scroll reaches the top of the page?

Asked

Viewed 273 times

2

I have all the code of a fixed sidebar that works perfectly, however I want that as soon as the scroll bar reaches the top of the page, the top itself, an element receive a CSS assignment through the . css().

Something along those lines:

$(window).scroll(function() {
if ($(window).scrollTop() == 0) {
$('#top-tabv2').css({'margin-top': '-1px'});
}
});

How can I proceed? From now on, thank you! =]

  • 1

    Your code seems to be correct. What problem is giving?

  • And that’s right, man! xD I hadn’t noticed this before, because I had given an error in the compilation and for that reason I thought the flaw was in the code, but, thank God, everything is working as expected! Thanks for the return!

1 answer

2


You can do it like this:

const execute = function () {
   console.log('Cheguei no topo!');
};

$(window).on('scroll', () => {
   if ($(this).scrollTop() === 0) execute();
});
  • Thanks for the answer too, man! It works! =]

  • Dispose. Don’t forget to click on the check icon on the left side of my message to mark your question as resolved. Take a vote as well. p

Browser other questions tagged

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