Search code examples
jquerysmooth-scrolling

jQuery move and accelerate to a direction for unknown distance (smooth scroll)


Imagine this code (simplified)

<img id="leftArrow"/>
<div style="overflow:hidden; width:300px">
  <img id="wideImage" style="width:1000px; position:absolute; left:0"/>
</div>
<img id="rightArrow"/>

that will result in something like this
alt text
(source: aximili at home.exetel.com.au)

How do you make #wideImage scroll smoothly to the left when #rightArrow is hovered?

I am after something like this

$('#right').hover(function() {
  //On hover, accelerate #wideImage to the left
}, function() {
  //On mouse out, decelerate to stop
});

Thanks in advance!


Solution

  • This sort of does it

    $('#right').hover(function() {
      $('#wideImage').animate({ left: '-=50' }, 250, 'easeInQuad', function () {
        $('#wideImage').animate({ left: '-=600' }, 250, 'linear', function () {
          $('#wideImage').animate({ left: '-=50' }, 250, 'easeOutQuad');
        });
      });
    }, function() {
      $('#wideImage').stop();
      $('#wideImage').animate({ left: '-=50' }, 250, 'easeOutQuad');
    });