title: JS : Event on ScrollDown and ScrollUp
body: <p>I currently have a animated sprite that walks when you press the left and right keys on the keyboard. What I would like to do is to make the sprite walk-right when you scroll down and walk left when you scroll up. I would also like to make the sprite stop walking when the user stops scrolling. Thanks in advance! </p>
  
  <p>I tried using the $(window).scroll function with variables of current and lastscroll positions, but it didn't work. </p>
  
  <pre><code>function walk(e) {
      var keyCode = e.keyCode;
      if (keyCode === 39) {
          key.right = true;
      } else if (keyCode === 37) {
          key.left = true;
      }
      if (key.right === true) {
          trans += 0;
          translate();
          sprite.classList.remove('left');
          sprite.classList.add('right');
          sprite.classList.add('walk-right');
      } else if (key.left === true) {
          trans -= 0;
          translate();
          sprite.classList.remove('right');
          sprite.classList.add('left');
          sprite.classList.add('walk-left');
      }
  }
  
  function stop(e) {
      var keyCode = e.keyCode;
      if (keyCode === 39) {
          key.right = false;
      } else if (keyCode === 37) {
          key.left = false;
      }
      if (key.right === false) {
          sprite.classList.remove('walk-right');
      } if (key.left === false) {
          sprite.classList.remove('walk-left');
      }
  }
  </code></pre>