javascript 未捕获的类型错误:无法使用“in”运算符在未定义中搜索“scrollLeft”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17184706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:24:15  来源:igfitidea点击:

Uncaught TypeError: Cannot use 'in' operator to search for 'scrollLeft' in undefined

javascriptjqueryjquery-animate

提问by AKG

According to the jQuery API Documentationand some examples found here, scrollLeft is a valid argument for animate(). However, I keep getting this error Uncaught TypeError: Cannot use 'in' operator to search for 'scrollLeft' in undefined.

根据jQuery API 文档和此处找到的一些示例,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);
});

Is there something simple and silly that I am overlooking? What am I doing wrong? Thanks :)

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

回答by adeneo

The window doesn't have a scrollbar, it belongs to the body or the documentElement (html tag) :

窗口没有滚动条,它属于正文或文档元素(html 标签):

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

Strange as it may seem you can get the windows scrollLeft property with css(), but when animating, you animate the body and html tags.

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