首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG动画与SMIL

SVG动画与SMIL
EN

Stack Overflow用户
提问于 2015-02-03 09:00:40
回答 1查看 535关注 0票数 1

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

以下是我已经尝试过的:

HTML/XML:

代码语言:javascript
复制
        <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:

在本例中,键按事件执行以下操作:

代码语言:javascript
复制
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动画在这种情况下更好?

我怎么才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2015-08-28 16:16:38

这太难了.我在这里捣乱了

代码语言:javascript
复制
$(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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28294933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档