自定义SVG进度条填充

时间:2015-06-12 11:29:18

标签: animation svg webkit progress-bar

在最简单的形式中,我想制作一个像this website这样的加载页面。

我想使用自定义SVG徽标(我在插图画家中制作)并在页面加载时水平填充徽标。

像SVG剪辑蒙版进度条(或其他)一样。

拜托,请帮助我!

谢谢,乔恩

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用渐变填充。

<linearGradient id="progress">
   <stop id="stop1" offset="0" stop-color="black"/>
   <stop id="stop2" offset="0" stop-color="grey"/>
</linearGradient>

您只需将两个offset元素的<stop>值更改为您想要的百分比 - 0-> 1或“0%” - >“100%”。例如:

示例函数可能是:

function setProgress(amt)
{
  amt = (amt < 0) ? 0 : (amt > 1) ? 1 : amt;
  document.getElementById("stop1").setAttribute("offset", amt);
  document.getElementById("stop2").setAttribute("offset", amt);
}

此方法适用于任何SVG元素,无论是文本,如下面的演示,还是由路径构成的复杂徽标。

function setProgress(amt)
{
  amt = (amt < 0) ? 0 : (amt > 1) ? 1 : amt;
  document.getElementById("stop1").setAttribute("offset", amt);
  document.getElementById("stop2").setAttribute("offset", amt);
}

  
// Simple test of setProgress().
// We step up from 0 to 1 using timeouts
val = 0;
doTimeout();

function doTimeout() {
  setProgress(val);
  val += 0.01;
  if (val <= 1) {
    setTimeout(doTimeout, 30);
  }
}
text {
  font: 'Times Roman', serif;
  font-size: 125px;
  fill: url(#progress);
}
<svg width="650" height="150">
  <defs>
    <linearGradient id="progress">
      <stop id="stop1" offset="0" stop-color="black"/>
      <stop id="stop2" offset="0" stop-color="grey"/>
    </linearGradient>
  </defs>

  <text x="20" y="120">TILL JANZ</text>
</svg>

相关问题