Search code examples
javascriptclassscrollretain

Javascript: Retain class if page refresh after scrolling


I'm using the following javascript to add a class to a top menu after the page has been scrolled 170px. This works great.

$(document).ready(function($) {
    $(window).scroll(function () {
        if ($(window).scrollTop() > 170) {
            $('header').addClass('shrink');
        }
        else{
            $('header').removeClass('shrink');
        }
    });
});

The problem happens on reloading the page past the 170px point, the menu gets the default class until the page is scrolled. This site has the same problem: http://metropolisspasalon.com/ The menu is black by default then turns white after scrolling. When the menu is white and you reload the page, it turns black again.


Solution

  • I found the answer by using this script: http://callmenick.com/post/animated-resizing-header-on-scroll Here is his "classie.js" script:

    /*!
    * classie v1.0.0
     * class helper functions
     * from bonzo https://github.com/ded/bonzo
     * MIT license
     *
     * classie.has( elem, 'my-class' ) -> true/false
     * classie.add( elem, 'my-new-class' )
     * classie.remove( elem, 'my-unwanted-class' )
     * classie.toggle( elem, 'my-class' )
     */
    
    /*jshint browser: true, strict: true, undef: true, unused: true */
    /*global define: false */
    
    ( function( window ) {
    
    'use strict';
    
    // class helper functions from bonzo https://github.com/ded/bonzo
    
    function classReg( className ) {
    return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
    }
    
    // classList support for class management
    // altho to be fair, the api sucks because it won't accept multiple classes at once
    var hasClass, addClass, removeClass;
    
    if ( 'classList' in document.documentElement ) {
    hasClass = function( elem, c ) {
    return elem.classList.contains( c );
    };
    addClass = function( elem, c ) {
    elem.classList.add( c );
    };
    removeClass = function( elem, c ) {
    elem.classList.remove( c );
    };
    }
    else {
      hasClass = function( elem, c ) {
    return classReg( c ).test( elem.className );
      };
      addClass = function( elem, c ) {
    if ( !hasClass( elem, c ) ) {
      elem.className = elem.className + ' ' + c;
    }
      };
      removeClass = function( elem, c ) {
    elem.className = elem.className.replace( classReg( c ), ' ' );
      };
    }
    
    function toggleClass( elem, c ) {
    var fn = hasClass( elem, c ) ? removeClass : addClass;
    fn( elem, c );
    }
    
    var classie = {
      // full names
      hasClass: hasClass,
      addClass: addClass,
      removeClass: removeClass,
      toggleClass: toggleClass,
      // short names
      has: hasClass,
      add: addClass,
      remove: removeClass,
      toggle: toggleClass
    };
    
    // transport
    if ( typeof define === 'function' && define.amd ) {
      // AMD
      define( classie );
    } else {
      // browser global
      window.classie = classie;
    }
    
    })( window );
    

    And here is the JS for the header:

        function init() {
            window.addEventListener('scroll', function(e){
            var distanceY = window.pageYOffset ||     document.documentElement.scrollTop,
                shrinkOn = 300,
                header = document.querySelector("header");
            if (distanceY > shrinkOn) {
                classie.add(header,"smaller");
            } else {
                if (classie.has(header,"smaller")) {
                    classie.remove(header,"smaller");
                }
            }
        });
    }
    window.onload = init();