jquery animate() 问题(语法?)

发布于 2024-08-30 15:44:24 字数 1026 浏览 1 评论 0原文

$('#somediv').stop(false, true)
             .animate({marginLeft: '-=' + e.width() + 'px'}, options.speed, function(){ options.onNewSlide() })
  • e.with() 返回 640
  • options.speed 包含 800
  • options.onNewSlide() 包含自定义回调函数

它在 Firefox 中工作正常。但我用 jQuery-Lint 对其进行了调试,因为它在 IE 中抛出了一些随机错误。 Lint 告诉我:

When I called animate(...) with your args, an error was thrown! TypeError: c.speed is not a function { message="c.speed is not a function",  more...}
You passed:  [Object {  marginLeft="-=640px"}, 800, function()]

它指示我发布的行。 我已经检查了 jQuery 文档,但我的语法没问题。

你知道我做错了什么吗?

PS:我使用 Google API 中的 jQuery 1.4.2,您可以在此处看到错误: http://meodai.ch/ slider/ (我知道代码正在构建中,但仍然)

编辑: 我尝试在新文件中仅使用动画重现错误: http://meodai.ch/ slider/index2.html 在那里它工作得很好!现在我迷路了。

$('#somediv').stop(false, true)
             .animate({marginLeft: '-=' + e.width() + 'px'}, options.speed, function(){ options.onNewSlide() })
  • e.with() returns 640
  • options.speed contains 800
  • options.onNewSlide() contains a a custom callback function

It works fine in Firefox. But i debugged it with jQuery-Lint because it was throwing some random error in IE. Lint tells me:

When I called animate(...) with your args, an error was thrown! TypeError: c.speed is not a function { message="c.speed is not a function",  more...}
You passed:  [Object {  marginLeft="-=640px"}, 800, function()]

and it indicates me the line i have posted.
I have checked the jQuery doc, but my syntax seams ok.

Do you know what I am doing wrong?

PS: I use jQuery 1.4.2 from the Google API you can see the error here: http://meodai.ch/slider/ (I know the code is under construction, but still)

edit:
I have tried to reproduce my error in a new file with just the animation: http://meodai.ch/slider/index2.html there it works very well! Now I am lost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肥爪爪 2024-09-06 15:44:24

在您的代码中,您有:

options = $.extend(defaultOptions, options)

optionsundefined 时,这会使用 defaultOptions 中的属性扩展 jQuery 对象。

defaultOptions 中有 speedeasing 属性,它们会覆盖 jQuery 的同名内部属性,从而导致错误。

结合 defaultOptionsoptions 的更安全方法是:

options = $.extend({}, defaultOptions, options);

In your code you have:

options = $.extend(defaultOptions, options)

When options is undefined, this extends the jQuery object with the properties from defaultOptions.

Within defaultOptions are speed and easing properties, which overwrite jQuery's internal properties of the same names, causing the error.

A safer way to combine defaultOptions and options would be:

options = $.extend({}, defaultOptions, options);
情未る 2024-09-06 15:44:24

我看到的一件事是您没有指定要使用的缓动函数。尝试

$('#somediv')
  .stop(false, true)
  .animate({marginLeft: '-=' + e.width() + 'px'}, 
    options.speed, 
    'linear',
    function(){ options.onNewSlide() })

One thing that I see is that you are not specifying the easing function to use. Try

$('#somediv')
  .stop(false, true)
  .animate({marginLeft: '-=' + e.width() + 'px'}, 
    options.speed, 
    'linear',
    function(){ options.onNewSlide() })
嘦怹 2024-09-06 15:44:24

我为我的参数创建了一个对象options。在选项对象内部,我重命名该选项后,我的问题就得到了解决。但为什么我在其他测试页上没有遇到同样的问题呢?

I have made an object options for my params. inside the option object i had speed: and easing: as soon as i renamed that options my problem was solved. But how does it comes that i don't have the same problem on my other test page?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文