【问题标题】:Animating width with jQuery使用 jQuery 动画宽度
【发布时间】:2017-05-05 13:21:06
【问题描述】:

大家好,

    $(".textF").animate({width: actualWidth + "px"}, durations[0]*1000);

快速提问。这很有效,因为我用 jQuery 得到了它。但是我有很多跨度的课程,如果我这样做是行不通的:

    var fElements=document.getElementsByClassName("textF");
    fElements[0].animate({width: actualWidth + "px"}, durations[0]*1000);

我得到的错误:

未捕获的 DOMException:无法在“元素”上执行“动画”:不支持部分关键帧。

【问题讨论】:

  • 你在你的页面中加入了jQuery,你为什么使用pure JS
  • 你这是什么意思?
  • 我的意思是你包括了jQuery,那么为什么你厌倦了使用getElementsByClassName而不是$('...')
  • 因为它没有将我的“var fElements”创建为数组。使用 getElementsByClassName 确实
  • 你没有得到我兄弟,但没关系;)

标签: javascript jquery html jquery-animate


【解决方案1】:

jquery 不适用于本机 javaScript 函数。

错误?- animate() 由 jquery 定义,而不是原生 Javascript

参考animate()jquery 文档

$(fElements[0]).animate({width: actualWidth + "px"}, durations[0]*1000);

$(fElements).eq(0).animate({width: actualWidth + "px"}, durations[0]*1000);

【讨论】:

  • animate函数中不需要使用px。默认单位是像素。
  • 谢谢!我真的很感激。有什么区别呢?我真的不明白。我知道这是关于 DOM 元素的,但是...
  • @DavidPradellCasellas 因为 jquery 声明与本机 js 声明不同。最好查看此线程以更清楚地了解 difference between dom and jquery
  • 非常感谢
  • @DavidPradellCasellas 永远欢迎..继续学习..快乐编码..:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 2014-06-27
  • 2011-07-31
  • 2011-03-03
  • 2011-10-07
相关资源
最近更新 更多