2017-05-05 113 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