未捕获的TypeError:无法使用'in'运算符在undefined中搜索'scrollLeft'

时间:2013-06-19 06:55:05

标签: javascript jquery jquery-animate

根据jQuery API Documentation和此处的一些示例,scrollLeft是animate()的有效参数。但是,我一直收到此错误Uncaught TypeError: Cannot use 'in' operator to search for 'scrollLeft' in undefined

$('#prev a, #next a').click(function() {
    $(window).animate({scrollLeft: 500}, 1000);
});

我忽略了一些简单而愚蠢的东西吗?我究竟做错了什么?谢谢:))

1 个答案:

答案 0 :(得分:18)

窗口没有滚动条,它属于body或documentElement(html标签):

$('#prev a, #next a').click(function() {
    $('body, html').animate({scrollLeft: 500}, 1000);
});

看起来很奇怪你可以使用css()获取windows scrollLeft属性,但是在制作动画时,你可以设置body和html标签的动画。