首頁 > web前端 > css教學 > 主體

如何用 CSS 或 Web 動畫取代已棄用的 SMIL 動畫?

Linda Hamilton
發布: 2024-10-25 17:34:32
原創
392 人瀏覽過

How Can I Replace Deprecated SMIL Animations with CSS or Web Animations?

懸停效果:CSS 動畫或網頁動畫

問題:
SMIL 的animate 標籤已棄用,CSS 懸停過渡需要更改

解決方案:
去掉set標籤,在element_tpl:hover偽類中加入CSS:

<code class="css">.element_tpl:hover {
    stroke-opacity: 0.5;
}</code>
登入後複製

縮放效果更改

問題:
在提交的更改時對元素進行動畫縮放幾次。

解決方案:
考慮以下選項:

  1. CSS 動畫: 使用CSS🎜>
  2. CSS 動畫:
使用CSS🎜>
<code class="css">@keyframes scale-animation {
    0% { transform: scale(1); }
    50% { transform: scale(1.12); }
    100% { transform: scale(1); }
}</code>
登入後複製

然後將動畫套用到元素:
<code class="css">.element_tpl {
    animation: scale-animation 0.5s infinite alternate;
}</code>
登入後複製
  1. 網頁動畫:
  2. 使用網頁動畫API 以程式控制縮放:
<code class="javascript">// Create a new animation
const animation = document.timeline.addAnimation();

// Define keyframes
const keyframes = [
    { transform: 'scale(1)', offset: 0 },
    { transform: 'scale(1.12)', offset: 0.5 },
    { transform: 'scale(1)', offset: 1 }
];

// Apply keyframes to the animation
animation.effect = keyframes;

// Target the element
animation.target = document.querySelector('.element_tpl');</code>
登入後複製

點擊時向上和向下縮放

問題:

讓元素在點擊時以動畫方式放大和縮小。

解決方案:

  1. CSS 過渡:
  2. 使用CSS 過渡觸發mousedown 和mouseup 事件的比例變化:
使用CSS 過渡觸發mousedown 和mouseup 事件的比例變化:
<code class="css">.element_tpl {
    transition: transform 0.2s;
}

.element_tpl:active {
    transform: scale(1.1);
}</code>
登入後複製
  1. 網頁動畫:
使用網頁動畫處理點擊事件並對應縮放元素的API:
<code class="javascript">// Add event listener
document.querySelector('.element_tpl').addEventListener('click', (event) => {

    // Create a new animation
    const animation = document.timeline.addAnimation();

    // Define keyframes
    const keyframes = [
        { transform: 'scale(1)', offset: 0 },
        { transform: 'scale(1.1)', offset: 0.2 },
        { transform: 'scale(1)', offset: 0.4 },
    ];

    // Apply keyframes to the animation
    animation.effect = keyframes;

    // Target the element
    animation.target = event.target;
});</code>
登入後複製

儲存SMIL 動畫
問題:

將SMIL 動畫傳送到CSS 或網頁動畫。
解決方案:

使用 Eric Willigers 建立的 SMIL polyfill:https://github.com/ericwilligers/svg-animation。這個polyfill將SMIL動畫轉換為Web動畫,提供了另一種實作它們的方法。

以上是如何用 CSS 或 Web 動畫取代已棄用的 SMIL 動畫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!