{"id":2044,"date":"2024-05-26T09:39:12","date_gmt":"2024-05-26T01:39:12","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=2044"},"modified":"2024-05-26T09:40:41","modified_gmt":"2024-05-26T01:40:41","slug":"element-getanimations","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=2044","title":{"rendered":"Element: getAnimations()"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2024\/05\/getAnimations-scaled.jpeg\" alt=\"getAnimations\" \/><\/p>\n<p><code>Element.getAnimations()<\/code> \u662f\u4e00\u79cd\u7528\u4e8e\u83b7\u53d6\u4e0e\u5143\u7d20\u5173\u8054\u7684\u6240\u6709\u52a8\u753b\uff08\u5305\u62ec CSS \u52a8\u753b\u548c Web \u52a8\u753b API \u52a8\u753b\uff09\u7684\u65b9\u6cd5\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b <code>Animation<\/code> \u5bf9\u8c61\u7684\u6570\u7ec4\u3002\u8fd9\u4e9b <code>Animation<\/code> \u5bf9\u8c61\u8868\u793a\u5143\u7d20\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u6216\u6302\u8d77\u7684\u52a8\u753b\u3002<\/p>\n<h3><a id=\"%E8%AF%AD%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8bed\u6cd5<\/h3>\n<pre><code class=\"language-javascript\">let animations = element.getAnimations();\n<\/code><\/pre>\n<h3><a id=\"%E8%BF%94%E5%9B%9E%E5%80%BC\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8fd4\u56de\u503c<\/h3>\n<ul>\n<li>\u4e00\u4e2a\u5305\u542b <code>Animation<\/code> \u5bf9\u8c61\u7684\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E6%A1%88%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u6848\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <code>Element.getAnimations()<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u548c\u64cd\u4f5c\u5143\u7d20\u7684\u52a8\u753b\u3002<\/p>\n<h4><a id=\"%E7%A4%BA%E4%BE%8B1%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%B9%B6%E6%9A%82%E5%81%9C%E6%89%80%E6%9C%89%E5%8A%A8%E7%94%BB\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u793a\u4f8b 1\uff1a\u83b7\u53d6\u5e76\u6682\u505c\u6240\u6709\u52a8\u753b<\/h4>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a HTML \u5143\u7d20\u6b63\u5728\u5e94\u7528 CSS \u52a8\u753b\u6216\u901a\u8fc7 Web \u52a8\u753b API \u52a8\u753b\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;style&gt;\n    @keyframes slide {\n      from {\n        transform: translateX(0);\n      }\n      to {\n        transform: translateX(100px);\n      }\n    }\n    .animated {\n      animation: slide 2s infinite;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div class=&quot;animated&quot;&gt;This is a sliding div.&lt;\/div&gt;\n  &lt;button id=&quot;pause&quot;&gt;Pause Animations&lt;\/button&gt;\n  &lt;button id=&quot;play&quot;&gt;Play Animations&lt;\/button&gt;\n\n  &lt;script&gt;\n    const animatedElement = document.querySelector('.animated');\n    const pauseButton = document.getElementById('pause');\n    const playButton = document.getElementById('play');\n\n    pauseButton.addEventListener('click', () =&gt; {\n      const animations = animatedElement.getAnimations();\n      animations.forEach(animation =&gt; {\n        animation.pause();\n      });\n    });\n\n    playButton.addEventListener('click', () =&gt; {\n      const animations = animatedElement.getAnimations();\n      animations.forEach(animation =&gt; {\n        animation.play();\n      });\n    });\n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e86\u4e00\u4e2a CSS \u52a8\u753b <code>slide<\/code> \u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u4e00\u4e2a\u5e26\u6709 <code>animated<\/code> \u7c7b\u7684 <code>div<\/code> \u5143\u7d20\u3002<\/li>\n<li>\u4f7f\u7528 <code>Element.getAnimations()<\/code> \u83b7\u53d6\u4e0e\u8be5\u5143\u7d20\u5173\u8054\u7684\u6240\u6709\u52a8\u753b\u3002<\/li>\n<li>\u901a\u8fc7\u70b9\u51fb &quot;Pause Animations&quot; \u6309\u94ae\uff0c\u53ef\u4ee5\u6682\u505c\u6240\u6709\u52a8\u753b\u3002<\/li>\n<li>\u901a\u8fc7\u70b9\u51fb &quot;Play Animations&quot; \u6309\u94ae\uff0c\u53ef\u4ee5\u6062\u590d\u6240\u6709\u52a8\u753b\u3002<\/li>\n<\/ol>\n<h4><a id=\"%E7%A4%BA%E4%BE%8B2%EF%BC%9A%E6%94%B9%E5%8F%98%E5%8A%A8%E7%94%BB%E6%92%AD%E6%94%BE%E9%80%9F%E5%BA%A6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u793a\u4f8b 2\uff1a\u6539\u53d8\u52a8\u753b\u64ad\u653e\u901f\u5ea6<\/h4>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>Element.getAnimations()<\/code> \u6765\u6539\u53d8\u52a8\u753b\u7684\u64ad\u653e\u901f\u5ea6\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;style&gt;\n    @keyframes slide {\n      from {\n        transform: translateX(0);\n      }\n      to {\n        transform: translateX(100px);\n      }\n    }\n    .animated {\n      animation: slide 2s infinite;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div class=&quot;animated&quot;&gt;This is a sliding div.&lt;\/div&gt;\n  &lt;button id=&quot;speed-up&quot;&gt;Speed Up Animations&lt;\/button&gt;\n  &lt;button id=&quot;slow-down&quot;&gt;Slow Down Animations&lt;\/button&gt;\n\n  &lt;script&gt;\n    const animatedElement = document.querySelector('.animated');\n    const speedUpButton = document.getElementById('speed-up');\n    const slowDownButton = document.getElementById('slow-down');\n\n    speedUpButton.addEventListener('click', () =&gt; {\n      const animations = animatedElement.getAnimations();\n      animations.forEach(animation =&gt; {\n        animation.playbackRate *= 2;\n      });\n    });\n\n    slowDownButton.addEventListener('click', () =&gt; {\n      const animations = animatedElement.getAnimations();\n      animations.forEach(animation =&gt; {\n        animation.playbackRate \/= 2;\n      });\n    });\n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>Element.getAnimations()<\/code> \u83b7\u53d6\u4e0e\u8be5\u5143\u7d20\u5173\u8054\u7684\u6240\u6709\u52a8\u753b\u3002<\/li>\n<li>\u901a\u8fc7\u70b9\u51fb &quot;Speed Up Animations&quot; \u6309\u94ae\uff0c\u53ef\u4ee5\u5c06\u52a8\u753b\u7684\u64ad\u653e\u901f\u5ea6\u52a0\u5feb\u4e00\u500d\u3002<\/li>\n<li>\u901a\u8fc7\u70b9\u51fb &quot;Slow Down Animations&quot; \u6309\u94ae\uff0c\u53ef\u4ee5\u5c06\u52a8\u753b\u7684\u64ad\u653e\u901f\u5ea6\u51cf\u6162\u4e00\u500d\u3002<\/li>\n<\/ol>\n<p><code>Element.getAnimations()<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u63a7\u5236\u548c\u7ba1\u7406\u4e0e\u5143\u7d20\u5173\u8054\u7684\u6240\u6709\u52a8\u753b\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u52a8\u6001\u5730\u63a7\u5236\u52a8\u753b\u7684\u884c\u4e3a\u548c\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Element.getAnimations() \u662f\u4e00\u79cd\u7528\u4e8e\u83b7\u53d6\u4e0e\u5143\u7d20\u5173\u8054\u7684\u6240\u6709\u52a8\u753b\uff08\u5305\u62ec CSS \u52a8&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[5],"class_list":["post-2044","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-css"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2044"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2044\/revisions"}],"predecessor-version":[{"id":2045,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2044\/revisions\/2045"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}