使用純 CSS 的方法,暫停或播放 CSS 動畫。是不是看起來應該是不可能的實現的;或者就算可以實現,也是一個很麻煩的實現方法,需要用大量的css樣式才可以實現。其實不然,在 CSS3 animation 中,就有這樣一個屬性可以做到暫停、播放動畫。本章就跟大家介紹如何用純CSS方式實現CSS動畫的暫停與播放效果? animation-play-state屬性介紹(詳解)。有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
animation-play-state屬性
animation-play-state: paused | running;
animation-play-state: 屬性定義一個動畫是否運作或暫停。可以透過查詢它來確定動畫是否正在運行。另外,它的值可以被設定為暫停和恢復的動畫的重播。
如果借助Javascript,我們可以實現控制CSS 動畫的運行和播放,以下列出部分關鍵程式碼:
html程式碼:
<div class="btn">stop</div> <div class="animation"></div>
css程式碼:
.animation { width: 100px; height: 100px; margin: 50px auto; background: deeppink; animation: move 2s linear infinite alternate; } @keyframes move { 0% { transform: translate(-100px, 0); } 100% { transform: translate(100px, 0); } } .btn { width: 50px; margin: 10px auto; text-align: center; border:1px solid #ddd; padding: 10px; border-radius: 5px; cursor:pointer; &:hover { background: #ddd; color: #333; } &:active { background: #aaa; } }
js程式碼:
document.querySelector('.btn').addEventListener('click', function() { let btn = document.querySelector('.btn'); let elem = document.querySelector('.animation'); let state = elem.style['animationPlayState']; if(state === 'paused') { elem.style['animationPlayState'] = 'running'; btn.innerText = 'stop'; } else { elem.style['animationPlayState'] = 'paused'; btn.innerText = 'play'; } });
效果圖(播放時與停止播放後):
純CSS 實作
在下面我們探討下,使用純CSS 的方式能否實現。
hover 偽類實作
使用 hover 偽類,在滑鼠停留在按鈕上面時,控制動畫樣式的暫停。
關鍵程式碼如下:
html程式碼:
<div class="btn stop">stop</div> <div class="animation"></div>
css程式碼:
.animation { width: 100px; height: 100px; margin: 50px auto; background: deeppink; animation: move 2s linear infinite alternate; } input { display: none; } @keyframes move { 0% { transform: translate(-100px, 0); } 100% { transform: translate(100px, 0); } } .btn { width: 50px; margin: 10px auto; text-align: center; border:1px solid #ddd; padding: 10px; border-radius: 5px; cursor:pointer; &:hover { background: #ddd; color: #333; } &:active { background: #aaa; } } .stop:hover ~ .animation { animation-play-state: paused; }
效果圖:
checked 偽類實作
之前的文章《有趣的CSS 題目(8):純CSS的導覽列Tab切換方案》也談過,使用radio 標籤的checked 偽類,加上實現純CSS 捕獲點擊事情。 並且利用被點擊的元素可以控制一些 CSS 樣式。實作如下:html程式碼:<input id="stop" type="radio" name="playAnimation"/> <input id="play" type="radio" name="playAnimation"/> <div class="box"> <label for="stop"> <div class="btn">stop</div> </label> <label for="play"> <div class="btn">play</div> </label> </div> <div class="animation"></div>css程式碼:
.animation { width: 100px; height: 100px; margin: 50px auto; background: deeppink; animation: move 2s linear infinite alternate; } input { display: none; } @keyframes move { 0% { transform: translate(-100px, 0); } 100% { transform: translate(100px, 0); } } .btn { width: 50px; margin: 10px auto; text-align: center; border:1px solid #ddd; padding: 10px; border-radius: 5px; cursor:pointer; &:hover { background: #ddd; color: #333; } &:active { background: #aaa; } } #stop:checked ~ .animation { animation-play-state: paused; } #play:checked ~ .animation { animation-play-state: running; }我們希望當#stop 和#play 兩個radio 被點擊時,給.animation 元素分別賦予animation-play-state: paused 或是animation-play-state: running 。而二者只能生效其一,所以需要給予兩個 radio 標籤相同的 name 屬性。 效果圖:
以上是如何用純CSS方式實現CSS動畫的暫停與播放效果? animation-play-state屬性介紹(詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Wufoo一直在集成方面非常出色。他們與特定應用程序(例如廣告系列顯示器,MailChimp和Typekit)進行集成,但他們也


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器