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

2

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

Something in this line:

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

How can I proceed? Right now, grateful! =]

    
asked by anonymous 30.12.2017 / 19:02

1 answer

2

You can do this:

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

$(window).on('scroll', () => {
   if ($(this).scrollTop() === 0) execute();
});
    
30.12.2017 / 19:20