jQuery的:动画scrollLeft [英] jquery: animate scrollLeft

查看:1004
本文介绍了jQuery的:动画scrollLeft的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对jquery还是很陌生,似乎无法弄清楚为什么我的代码无法正常工作.我有一个水平布局,想使用scrollLeft()函数(与此代码完美配合)

I'm quite new to jquery and can't seem to figure out why my code isn't working. I have a horizontal layout and want to use the scrollLeft() function (which works perfect with this code)

$("#next").click(function() {
    currentElement = currentElement.next();
    scrollTo(currentElement);    
});

function scrollTo(element) {
    $(window).scrollLeft(element.position().left);
}

但理想情况下,我想对此进行动画处理,以便在单击#next时向左滚动功能提供很好的动画效果

But ideally, I would like to animate this so that when #next is clicked there is a nice animated effect to the scroll left function

$("#next").click(function() {
    currentElement = currentElement.next();
    scrollTo(currentElement);    
});

function scrollTo(element) {
    $(window).animate({scrollLeft: element.position().left}, 750);
}

但无济于事.我在做什么错了?

But to no avail. What am I doing wrong?

推荐答案

您会想要这样的东西:

You'll want something like this:


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

我相信这应该可行,它是从scrollTop函数采用的.

I believe this should work, it's adopted from a scrollTop function.

这篇关于jQuery的:动画scrollLeft的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆