How to make an event that runs when the scroll reaches the very top in the block?
2 answers
$(function() { var block = $('.block') , console = $('.console'); $(window).scroll(function(e) { var offset = $(window).scrollTop() - block.offset().top; if (offset < 0) { console.text('Выше блока'); } else if (offset > 0) { console.text('Ниже блока'); } else { console.text('На блоке'); } }); }); .container { height: 1000px; } div { border: 1px solid; margin: 16px; padding: 16px; } .block { height: 64px; margin-top: 48px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="block">Block</div> <span class="console"></span> </div> |
I’m certainly not an expert, but there’s a popular opinion that it’s better to use a timer instead of a regular scrolling event.
|