Today, We want to share with you add class on scroll jquery.In this post we will show you add/remove class when scrolling over section / sticky sidebar, hear for Add/remove class with jquery based on vertical scroll? we will give you demo and example for implement.In this post, we will learn about Simple Jquery Scroll To Top with an example.
Add/remove class with jquery based on vertical scroll?
Example 1: index.html
<html> <title>This is Pakainfo - www.pakainfo.com</title> <head> <style> body { height: 2500px; } .sidebar div { width: 150px; height: 250px; float: right; position: fixed; z-index: 1; } .sidebar ul { list-style-type: none; padding-left: 0; position: fixed; } .normal { font-size: 13px; background: gray; color: white; text-transform: uppercase; width: 80px; } .active { width: 140px; background-color:orange; font-size: 25px; text-transform: uppercase; color: white; } </style> </head> <body> <div class="sidebar"> <ul> <li id="pop" class="normal">Home</li> <li class="normal">Programs</li> <li class="normal">Replay</li> </ul> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function(){ $(window).on("scroll",function() { if($(this).scrollTop() > 500) $("#pop").removeClass("normal").addClass("active"); else $("#pop").removeClass("active").addClass("normal"); }) }) </script> </body> </html>
Add/remove class on scroll down and scroll up
Example 2:
<div class="box">1</div> //After scroll down add class box-hover <div class="box box-hover">1</div> <script> $(document).ready(function() { $(window).scroll(function(){ if ($(this).scrollTop() > 50) { $('.box').addClass('box-hover'); } else { $('.box').removeClass('box-hover'); } }); }); </script>
Example 3:
Add or remove class with JQuery based on vertical scroll
<script> $(window).scroll(function() { if ($(this).scrollTop() > 150){ $('header').addClass("small"); } else { $('header').removeClass("small"); } }); </script>
header { height: 50px } header .menu a { height: 1.2em } .small { height: 25px } .small .menu a { height: 1em }
I hope you get an idea about add/remove class when scrolling over section.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.