本篇內(nèi)容介紹了“分析JavaScript中CSS關(guān)鍵幀的強(qiáng)大功能”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),雞西企業(yè)網(wǎng)站建設(shè),雞西品牌網(wǎng)站建設(shè),網(wǎng)站定制,雞西網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,雞西網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
要使用Web Animation API為關(guān)鍵幀動(dòng)畫設(shè)置動(dòng)畫,只需animate()在元素上調(diào)用該函數(shù):
1 |
|
該函數(shù)接受兩個(gè)參數(shù):
keyframes:包含所需CSS關(guān)鍵幀的JavaScript表示的文字?jǐn)?shù)組
keyframeOptions:包含動(dòng)畫的其他設(shè)置的文字,例如緩動(dòng),持續(xù)時(shí)間,填充模式等。
看一下下面的簡(jiǎn)單示例,該示例使用 animate()函數(shù)而不是CSS關(guān)鍵幀來(lái)渲染動(dòng)畫:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
如果我們使用純CSS聲明上面的內(nèi)容,它看起來(lái)像這樣:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
正如您所看到的,這兩種語(yǔ)法非常相似,如果您已經(jīng)熟悉CSS關(guān)鍵幀,那么將它移植到JavaScript就沒(méi)有問(wèn)題。與JavaScript版本的一些區(qū)別值得記住:
在JavaScript版本中,屬性字符串值應(yīng)該在引號(hào)(transform: 'translateX(0)')中
帶連字符的屬性名稱必須轉(zhuǎn)換為camelCase而不是(borderRadius: 0)。
逗號(hào)而不是分號(hào)應(yīng)該跟蹤每個(gè)屬性聲明(最后一個(gè)屬性除外)
默認(rèn)情況下,使用JavaScript設(shè)置的關(guān)鍵幀在播放時(shí)均勻分布,每個(gè)關(guān)鍵幀的時(shí)間相同。但是,通過(guò)offset在關(guān)鍵幀中添加屬性,我們可以設(shè)置該關(guān)鍵幀應(yīng)該開始播放的點(diǎn),例如60%標(biāo)記的0.6,類似于使用純CSS的關(guān)鍵幀。
該animate()方法的第二個(gè)參數(shù)是一個(gè)文字,可以很好地調(diào)整動(dòng)畫的行為。許多選項(xiàng)直接從CSS的animation-*屬性映射,例如“ animation-delay”,“ animation-fill-mode”等。所有屬性都是可選的,如果沒(méi)有設(shè)置,則回退到默認(rèn)值:
| property | CSS Property Equivalent | Description |
|---|---|---|
| id | none | Option that sets the name of this Animation to refer back to later in your code. |
| delay | animation-delay | The delay (integer) before the animation starts in milliseconds. Defaults to 0s. |
| direction | animation-direction | Defines whether the animation should play as normal, in reverse, or alternate between the two. Possible values are:
|
| duration | animation-delay | The duration (integer) of the animation in milliseconds, such as 1000. Default to 0 (no animation, jumps to last frame). |
| easing | animation-timing-function | Sets the easing function used to animate the @keyframe animation. Valid values include "ease", "ease-in", "ease-in-out","linear", "frames(integer)" etc. Defaults to "linear". |
| endDelay | n/a | The number of milliseconds to delay after the end of an animation. This is useful when sequencing multiple animations based on the end time of another animation. Defaults to 0. |
| fill | animation-fill-mode | Defines how the animation should apply styles to its target when the animation isn't playing anymore. Defaults to "none". Possible values are:
|
| iterationStart | n/a | Sets the point in the iteration the animation should start. The value should be a positive, floating point number. In an animation with 1 iteration, a iterationStart value of 0.5 starts the animation half way through. In an animation with 2 iterations, a iiterationStart value of 1.5 starts the animation half way through the 2nd iteration etc. Defaults to 0.0. |
| iterations
| animation-iteration-count | Sets the number of times the animation should run before stopping. A value of Infinity means forever. Defaults to 1. |
這是我在上面的例子中使用的keyframeOptions參數(shù):
1 2 3 4 5 6 |
|
如果我們想使用動(dòng)畫速記屬性在CSS中定義相同的選項(xiàng),它將如下所示:
1 |
|
使用Animation API創(chuàng)建關(guān)鍵幀動(dòng)畫的部分優(yōu)點(diǎn)是可以按需操作結(jié)果,例如暫停,跳過(guò)或掛鉤到動(dòng)畫的事件處理程序。執(zhí)行所有這些操作的第一步是在調(diào)用animate()方法時(shí)將動(dòng)畫分配給變量:
var myanimation = Element.animate(keyframes, keyframeOptions)
這將創(chuàng)建對(duì)Animation對(duì)象實(shí)例的引用,以允許我們通過(guò)各種公開的屬性和方法來(lái)操作動(dòng)畫:
1 2 3 4 |
|
這是修改后使用控件進(jìn)行回放的原始示例:
請(qǐng)注意,在此示例中,我animate()立即調(diào)用目標(biāo)元素,這將導(dǎo)致動(dòng)畫立即運(yùn)行。為了防止這種情況,我pause()隨后調(diào)用了該 方法。這是您希望手動(dòng)控制動(dòng)畫時(shí)使用的常用模式:
1 2 3 4 5 6 7 |
|
下面列出了動(dòng)畫對(duì)象實(shí)例的屬性,方法和事件處理程序,如上所述,在為animate()方法分配引用時(shí)創(chuàng)建:
屬性
currentTime: Gets or sets the current time value of the animation in milliseconds.
effect: Gets or sets the target effect of an animation. Support for this property is currently limited in all browsers.
finished: A promise object that's resolved when the animation has completed. Support for this property is currently limited in all browsers.
id: Gets or sets a string used to identify the animation.
playbackRate: Integer that gets or sets a playback rate of the animation. For example, 1=normal, 0 = pause, 2 = double, -1 = backwards etc.
playState: Read-only property that returns the current state of the animation: "idle", "pending", "running", "paused", or "finished".
ready: A promise object that's resolved when the animation is ready to be played. Support for this property is currently limited in all browsers.
startTime: Floating point number that gets or sets the current time (in milliseconds) of the animation.
timeline: Gets or sets the current timeline of the animation. Defaults to the document timeline (document.timeline). Support for this property is currently limited in all browsers.
方法
cancel(): Cancels the animation.
finish(): Immediately completes an animation.
pause(): Pauses an animation.
play(): Plays an animation.
reverse(): Reverses the current direction of the animation and plays it.
事件處理程序
oncancel: Triggered when the animation is canceled, such as by calling the cancel() method.
onfinish: Triggered when the animation is finished, such as by calling the finish() method.
通過(guò)操作currentTime屬性,下面為我們看到的基本動(dòng)畫添加了一個(gè)簡(jiǎn)單的擦除器:
我創(chuàng)建了一個(gè)HTML5 Range Slider來(lái)用作scrubber。當(dāng)動(dòng)畫首次運(yùn)行(自動(dòng))時(shí),動(dòng)畫的currentTime 屬性值將持續(xù)傳送到滑塊,以使兩者同步。目前沒(méi)有“onprogress”事件處理程序或類似的東西(據(jù)我所知)只在WAAPI動(dòng)畫運(yùn)行時(shí)運(yùn)行代碼,所以我用它 來(lái)監(jiān)視動(dòng)畫的進(jìn)度。動(dòng)畫結(jié)束后,我利用WAAPI事件調(diào)用并不必要地停止更新滑塊。
requestAnimationFrame()onfinishcancelAnimationFrame()
每當(dāng)用戶與Ranger Slider交互時(shí),我都會(huì)更新WAAPI動(dòng)畫以與滑塊同步:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
當(dāng)用戶按下滑塊時(shí),我暫停動(dòng)畫并更新滑塊的值以與動(dòng)畫的currentTime 屬性同步。當(dāng)用戶拖動(dòng)滑塊時(shí),反過(guò)來(lái)發(fā)生 - 我同步currentTime屬性以反映滑塊的值,因此前者依賴于后者。最后,當(dāng)用戶將鼠標(biāo)放在滑塊上時(shí),我恢復(fù)動(dòng)畫的自動(dòng)播放。
在下一個(gè)示例中,我將使用WAAPI一次演示動(dòng)畫多個(gè)元素,并在它們?nèi)拷Y(jié)束后執(zhí)行動(dòng)作。
注意:在撰寫本文時(shí),即使在Chrome和FF中,對(duì)WAAPI 承諾的原生支持也很不穩(wěn)定 。我不得不使用 Web Animation Next Polyfill來(lái)使這個(gè)功能在瀏覽器中運(yùn)行。
這里沒(méi)有什么太復(fù)雜的事了。基本上我循環(huán)并調(diào)用animate() 標(biāo)題中的每個(gè)字母,并將每個(gè)Animation對(duì)象實(shí)例存儲(chǔ)在數(shù)組中。有了這個(gè)數(shù)組,我可以根據(jù)需要循環(huán)播放一系列動(dòng)畫。每個(gè)動(dòng)畫的finished屬性都返回一個(gè) Promise,該動(dòng)畫在動(dòng)畫完成播放時(shí)被解析,我利用 Promise.all()在所有動(dòng)畫完成播放時(shí)重置整個(gè)動(dòng)畫。
Animation()構(gòu)造函數(shù)創(chuàng)建動(dòng)畫到目前為止,我只使用animate()對(duì)象直接在一個(gè)元素上創(chuàng)建了WAAPI動(dòng)畫,該元素返回一個(gè)Animation對(duì)象實(shí)例。我會(huì)失職但更不用說(shuō)你也可以使用Animation() 構(gòu)造函數(shù)來(lái)完成同樣的事情。
注意:Animation()在撰寫本文時(shí),即使在Chrome和FF中,本機(jī)支持也是不穩(wěn)定的。我不得不使用 Web Animation Next Polyfill來(lái)使這個(gè)功能在瀏覽器中運(yùn)行。
有了警告,這里是如何調(diào)用 Animation() 構(gòu)造函數(shù):
1 |
|
該函數(shù)接受兩個(gè)參數(shù):
effect:動(dòng)畫效果。在撰寫本文時(shí),僅keyframeEffect支持該對(duì)象。
timeline:動(dòng)畫時(shí)間軸。在撰寫本文時(shí),僅document.timeline支持。
讓我們看一下如何使用一個(gè)簡(jiǎn)單的例子:
這是JavaScript代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
新KeyframeEffect()對(duì)象是一個(gè)一體化對(duì)象,它包含一個(gè)位置的動(dòng)畫的所有設(shè)置,從目標(biāo)元素,要使用的關(guān)鍵幀到關(guān)鍵幀選項(xiàng)。
“分析JavaScript中CSS關(guān)鍵幀的強(qiáng)大功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前文章:分析JavaScript中CSS關(guān)鍵幀的強(qiáng)大功能
標(biāo)題來(lái)源:http://www.chinadenli.net/article26/gicpjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、服務(wù)器托管、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)