jquery:animate scrollLeft

我很新的jQuery,似乎无法弄清楚为什么我的代码不工作。 我有一个水平的布局,并希望使用scrollLeft()函数(这是完美的代码)

$("#next").click(function() { currentElement = currentElement.next(); scrollTo(currentElement); }); function scrollTo(element) { $(window).scrollLeft(element.position().left); } 

但理想情况下,我想animation这个,所以当#next被点击有一个很好的animation效果滚动左function

 $("#next").click(function() { currentElement = currentElement.next(); scrollTo(currentElement); }); function scrollTo(element) { $(window).animate({scrollLeft: element.position().left}, 750); } 

但无济于事。 我究竟做错了什么?

你会想要这样的东西:

 $("#next").click(function(){ var currentElement = currentElement.next(); $('html, body').animate({scrollLeft: $(currentElement).offset().left}, 800); return false; }); 

我相信这应该工作,它从scrollTop函数采用。

首先,我应该指出,如果你做了这么多的CSSanimation可能是最好的,但我结束通过包装.scrollLeft里面.animate结束得到所需的效果

 $('.swipeRight').click(function() { $('.swipeBox').animate( { scrollLeft: '+=460' }, 1000); }); $('.swipeLeft').click(function() { $('.swipeBox').animate( { scrollLeft: '-=460' }, 1000); }); 

第二个参数是速度,如果您使用某种types的平滑滚动,还可以添加第三个参数。