【问题标题】:I'm using javascript to make svg text show up after the animation (vivus.js) is finished我正在使用 javascript 在动画(vivus.js)完成后显示 svg 文本
【发布时间】:2017-01-17 14:24:42
【问题描述】:

我现在正在使用 Vivus.js 制作 svg 动画。

这是一个 SVG 线条动画,带有一些文本描述,位于 svg 的文本标签内。

因为vivus.js不能处理文字动画,我只是简单的先把文字的不透明度设为0,等svg线条动画完成后,再把文字的不透明度设为1,使其可见。

这是我在codepen 中导入 vivus.min.js 后放置的 .js 代码的主要部分:

// Making a svg line animation through Vivus.js, which works fine

var svg_product =
new Vivus('product', {
    type: 'scenario',
    duration: 100
});

// Make the <g id="text"> invisible before the animation is finished

var hidefirst = document.getElementById("text");
hidefirst.style.opacity = 0;

// Detect if the line animation is finished using strokeDashoffset

var line = document.getElementById("XMLID_202_");
var lineDetect = window.getComputedStyle(line).strokeDashoffset;

// After the line animation is finished, make <text> visible
// But the code seems doesn't work

if (lineDetect == "0px") {
    hidefirst.style.opacity = 1;
}

代码的最后一部分是我的问题。

我认为代码不起作用,因为我编写的“if 条件”在页面加载的一开始就被评估,即 False

因此我的问题是,如何正确跟踪 svg 行的 strokeDashoffset,以便在 window.getComputedStyle(line).strokeDashoffset 变成“0px”?

谢谢!

【问题讨论】:

    标签: javascript svg vivus


    【解决方案1】:

    您不需要检测动画是否完成,Vivus 构造函数采用第三个参数,该参数是在动画结束时调用的函数。

    var svg_product =
      new Vivus('product', {
                  type: 'scenario',
                  duration: 100 },
                function() {
                  // called after the animation completes
      })
    

    你可以使用那个或播放方法,例如

    svg_product.play(function() {
      // called after the animation completes
    })
    

    这一切都来自Vivus github documentation

    【讨论】:

    • 感谢您的回答!原来我没有通读文档。但是现在我学会了一个叫做“回调”的新概念!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多