Add a class to body tag after certain amount of scroll

Add a class to the body tag or any other element using jQuery after a certain amount of scroll or webpage load; you need to do it like below:

<script>
$(document).ready(function(){
    $(window).scroll(function() {    
	addScrollClass();
    });
    addScrollClass();

    function addScrollClass() {
	var headerSec = $("body");   
	var scrollFtop = $(window).scrollTop();
	if ( scrollFtop >= 50 ) { // change 50 with your number
		headerSec.addClass("scroll");
	} else {
		headerSec.removeClass("scroll");
	}
    }
});
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *