• Skip to primary navigation
  • Skip to main content
Matthew Dunbar

Matthew Dunbar

Hire Me
  • About
  • Portfolio
  • Blog
    • General
    • HTML
    • jQuery
    • PHP
    • Optimization
  • Hire Me
You are here: Home / jQuery / How to Add A Class to Header on Scroll with jQuery

How to Add A Class to Header on Scroll with jQuery

February 17, 2017


var header = $(".header");
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();
        if (scroll >= 300) {
            header.addClass("scrolled");
        } else {
            header.removeClass("scrolled");
        }
    });

Filed Under: jQuery, Learning

Go Back: What Programs and Services Does a Web Developer Use?
Next Up: Simple jQuery Slider without Controls
© 2025 Matthew Dunbar - All Rights Reserved Back to Top