Check scroll top value in if statement - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:scrollTop

Description

Check scroll top value in if statement

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $(window).scroll(function() {// www.ja  va  2  s  .c o  m
    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;">test.</p>

<div>This is a test. This is a test. This is a test.</div>

</body>
</html>

Related Tutorials