jQuery scrollTop() Check scroll top value

Description

jQuery scrollTop() Check scroll top value

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//from   w  w  w  . j  av  a2 s  .com
<script>
$(document).ready(function() {
  $(window).scroll(function() {
    if ($(document).scrollTop() > 50) {
      $("p").addClass("test");
    } else {
      $("p").removeClass("test");
    }
  });
});
</script>
<style>
.test {
  background-color: yellow;
}
</style>
</head>
<body style="height:1550px;">

<p>Scroll down this page.</p>

<p style="position:fixed;">
When you have scrolled 50 pixels from the top, add the class "test" 
(yellow background color) to the paragraph. 
Scroll up again to remove the class.</p>

</body>
</html>



PreviousNext

Related