2017-05-05 111 views
0

大家好,宽度设置动画使用jQuery

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

快速的问题。这工作,因为我用jQuery得到它。但是,我有很多的跨度与阶级,如果我做这种方式行不通:

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

的错误,我得到:

未捕获抛出:DOMException:未能执行的“动画” '元素':部分关键帧不受支持。

+0

您在页面中包含'jQuery' ,为什么你使用'纯JS'? –

+0

你是什么意思? –

+0

我的意思是你包含'jQuery',那么为什么你厌倦了'getElementsByClassName'而不是'$('...')'? –

回答

4

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); 
+0

在'animate'函数中不需要使用'px'。默认单位是像素。 –

+0

谢谢!我真的很感激。虽然有什么区别?我真的不明白。我知道这是关于DOM元素,但... –

+0

@DavidPradellCasellas,因为jquery声明不同于本机js声明。更好的看到这个线程更清楚地说明[dom和jquery之间的区别](http://stackoverflow.com/questions/ 6974582/jquery-object-and-dom-element) – prasanth