【问题标题】:SVG animation with SMIL带有 SMIL 的 SVG 动画
【发布时间】:2015-04-02 09:56:06
【问题描述】:

我有一些关于使用 smil SVG 动画和使用 javascript/jquery otf 更改内容的问题。

这是我已经尝试过的:

HTML/XML:

        <svg version="1.1" id="story_animation_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
        viewBox="0 0 400 500" enable-background="new 0 0 400 500" xml:space="preserve" style="background-color:#4B4542;">

            <g id="story_1">
                <path id="story_1_1" style="fill:none; stroke:#fff; stroke-width:2;" d="M200,100z">
                    <animate id="story_1_1_1"
                             attributeName="d" 
                             from="M200,100l0,0" to="M200,100l0,380" dur="1s"
                             begin="0s;story_1_1_2.end"
                             end=""
                    />
                    <animate id="story_1_1_2" 
                             attributeName="d" 
                             from="M200,100l0,380" to="M200,100l0,0" dur="1s"
                             begin="story_1_1_1.end"
                             end=""
                    />
                </path>
                <path id="story_1_2" style="fill:none; stroke:#fff; stroke-width:2;" d="M200,500z">
                    <animate id="story_1_2_1" 
                             attributeName="d" 
                             from="M200,500l0,0" to="M200,500l0,-380" dur="1s"
                             begin="story_1_1_1.begin+1s;story_1_2_2.end"
                             end=""
                    />
                    <animate id="story_1_2_2" 
                             attributeName="d" 
                             from="M200,500l0,-380" to="M200,500l0,0" dur="1s"
                             begin="story_1_2_1.end"
                             end=""
                    />
                </path>
            <g/>

            <g id="story_2">
                <path id="story_2_1" style="fill:none; stroke:#fff; stroke-width:2; stroke-dasharray:795.8291015625,795.8291015625; stroke-dashoffset:795.8291015625;" d="M200,100l86,50l-172,100l172,100l-172,100l86,50">
                    <animate id="story_2_1_1" 
                             attributeName="stroke-dashoffset" 
                             from="795.8291015625" to="-795.8291015625" dur="2s"
                             begin=""
                             calcMode="spline"
                             keySplines=".28 .25 .265 .25"
                             keyTimes="0;1"
                             end=""
                    />
                    <animate id="story_2_1_2" 
                             attributeName="stroke-dashoffset" 
                             from="795.8291015625" to="0" dur="1s"
                             begin=""
                             calcMode="spline"
                             keySplines=".28 .25 .265 .25"
                             keyTimes="0;1"
                             end=""
                    />
                </path>
            <g/>

        </svg> 

JS/jQuery:

在这种情况下,按键事件执行以下操作:

document.getElementById( 'story_1_1_1' ).attributes[ 5 ].value = '';

document.getElementById( 'story_1_1_1' ).attributes[ 6 ].value ='story_1_1_1.end';

document.getElementById( 'story_1_2_1' ).attributes[ 6 ].value ='story_1_2_1.end';

document.getElementById( 'story_2_1_1' ).attributes[ 5 ].value= 'story_1_1_2.end;story_2_1_1.end';

赞赏的结果:

我想实现:

  • 有一个不断运行的动画循环动画
  • 在事件触发时(例如单击、按键、...)在当前迭代结束时结束当前正在运行的动画
  • 在第一个动画结束时开始第二个 ( - 重复更多动画) (- 理想情况下反向做同样的事情:从动画 2 到 1)

实际结果:

到目前为止发生了什么:

  • 动画 1 循环运行良好
  • 按键时:
  • 案例 1(您在顶行下降时按下):
    • 有效
  • 案例 2(您在顶行上升时按下):
    • 静止动画再迭代一次然后停止

我愿意接受不同的方法和/或如何做到这一点的想法。 SMIL 至少在一开始似乎是一种快速简便的方法,而且在移动设备上的性能方面也很出色。也许在这种情况下css3动画更好?

我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery svg smil


    【解决方案1】:

    这很难......我在这里搞砸了:

    $(window).keypress(function(e) {//spacebar trigger
        if (e.keyCode === 0 || e.keyCode === 32) {
            $('#story_1_1_1, #story_1_1_2, #story_1_2_1, #story_1_2_2').attr('end', 'story_2_1_1.begin');
            $('#story_2_1_1').attr('begin', '0s;story_2_1_2.end');
        }
    });
    

    https://jsfiddle.net/kt7gax2a/2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 2017-07-01
      • 1970-01-01
      • 2016-09-18
      • 2018-03-06
      • 1970-01-01
      • 2019-10-07
      • 2019-10-05
      相关资源
      最近更新 更多