使用javascript "onload "启动SVG动画

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用javascript "onload "启动SVG动画相关的知识,希望对你有一定的参考价值。

我有一些SVG动画代码,但是当我打开我的网站时,经常会在加载的时候启动动画,而不是以它的完整形式。

let refreshRate = Math.random() *1000 + 50; // 10 times per second
function rand(max){
  return Math.floor(Math.random() * (max+1));
}
setInterval(function(){document.getElementById("RectElement").setAttribute("fill","rgb("+rand(255)+", "+rand(255)+", "+rand(255)+")");},refreshRate);
<!--start animation-->

<?xml version="1.0" standalone="no"?>
<svg width="8cm" height="3cm" viewBox="0 0 800 300" xmlns="http://www.w3.org/2000/svg">
  <desc>Example anim01 - demonstrate animation elements</desc>
  <rect x="1" y="1" width="900" height="400"
        fill="none" stroke="none" stroke-width="2" />
  <!-- The following illustrates the use of the 'animate' element
        to animate a rectangles x, y, and width attributes so that
        the rectangle grows to ultimately fill the viewport. -->
  <rect id="RectElement" x="300" y="100" width="300" height="100"
        fill="rgb(0,255,0)"  >
    <animate attributeName="fill" begin="0" dur="1.3"
   repeatCount="indefinite"/>
    <animate attributeName="x" begin="0s" dur="1.3s"
             fill="freeze" from="300" to="0" repeatCount="indefinite"/>
    <animate attributeName="y" begin="0s" dur="1.3s"
             fill="freeze" from="100" to="0" repeatCount="indefinite"/>
    <animate attributeName="width" begin="0s" dur="1.3s"
             fill="freeze" from="300" to="800" repeatCount="indefinite"/>
    <animate attributeName="height" begin="0s" dur="1.3s"
             fill="freeze" from="100" to="300" repeatCount="indefinite"/>
    
    
  </rect>
  <!-- Set up a new user coordinate system so that
        the text string's origin is at (0,0), allowing
        rotation and scale relative to the new origin -->
  <g transform="translate(100,100)" >
    <!-- The following illustrates the use of the 'set', 'animateMotion',
         'animate' and 'animateTransform' elements. The 'text' element
         below starts off hidden (i.e., invisible). At 3 seconds, it:
           * becomes visible
           * continuously moves diagonally across the viewport
           * changes color from blue to dark red
           * rotates from -30 to zero degrees
           * scales by a factor of three. -->
    <text id="TextElement" x="0" y="0"
          font-family="Verdana" font-size="35.27" visibility="hidden"  >
      Buy our crap!
      <set attributeName="visibility" to="visible"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      <animateMotion path="M 0 0 L 100 100"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      
      <animateTransform attributeName="transform"
           type="rotate" from="-30" to="0"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
      <animateTransform attributeName="transform"
           type="scale" from="0.5" to="2" additive="sum"
           begin="0s" dur="1.3s" fill="freeze" repeatCount="indefinite"/>
    </text>
  </g>
</svg>

当我打开页面时,经常发生的情况是,框体保持在最小的尺寸,并快速变换颜色,直到页面完全加载,最后才正常启动。然而这导致网站加载速度极慢。有没有什么办法(使用JS),让动画只在页面完全加载后才开始?

答案

你可以把你的代码包在一个函数中,用这个语法在文档加载时调用它。

function rand(max){
  return Math.floor(Math.random() * (max+1));
}

function init() {
  let refreshRate = Math.random() *1000 + 50; // 10 times per second
  setInterval(function(){document.getElementById("RectElement").setAttribute("fill","rgb("+rand(255)+", "+rand(255)+", "+rand(255)+")");},refreshRate);
}

window.document.onload = init;

以上是关于使用javascript "onload "启动SVG动画的主要内容,如果未能解决你的问题,请参考以下文章

请问一个网页中可以用两个"onload"吗?

两个javascript功能块,都是window.onload,怎样才能让多个功能块在一个页面都正常运行?

请问BODY中的ONLOAD是啥意思?

jQuery onload函数

谷歌浏览器window.onload事件不响应,Safari,Firefox都可以。

页面同时ONLOAD多个函数如何解决呀?