We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9995
    • 1,613 Posts
    I have got a fixed div and when starting to scroll I need the div to add a class.

    I have this below but it doesn't work.
    http://fiddle.jshell.net/fourroses666/ztysfz9u/

    Anyone an idea how to fix?
      Evolution user, I like the back-end speed and simplicity smiley
      • 42562
      • 1,145 Posts
      Something like? http://fiddle.jshell.net/ztysfz9u/3/
      var currentScrollPos = 0;
      $(window).scroll(function () {
          var topScrollPos = $(this).scrollTop();
          if (currentScrollPos > topScrollPos) {
              //Scrolling Down
              $(".top").addClass("smaller");
          } else {
              //Scrolling Up
              $(".top").removeClass("smaller");
          }
          currentScrollPos = topScrollPos;
      });
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
        • 9995
        • 1,613 Posts
        Hey thanks, but I need to add the class when reaching position 1px from the top.
        So when scrolling it adds the class. when scrolling up it keeps the class till we reach 0 px from the top.
          Evolution user, I like the back-end speed and simplicity smiley
          • 9995
          • 1,613 Posts
          I found something
          http://jsfiddle.net/sinky/S8Fnq/

          $(window).on("scroll touchmove", function () {
          	$('.top').toggleClass('smaller', $(document).scrollTop() > 0);
          });
          
            Evolution user, I like the back-end speed and simplicity smiley