Edit This Code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<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>


Result:
Try it Yourself - © w3schools.com
Privacy Policy