0

我正在使用 vivus.js 为重复多次的 svg 设置动画(作为 php 循环的一部分)。

我正在寻找一种交错动画的方法,以便 svg id 'hexx--0' 首先进行动画处理,然后在完成时,svg id 'hexx--1' 进行动画处理,依此类推。ID 号在 php 循环中递增。

我试图将它们链接在一个函数中,该函数在第一个动画完成时触发,如下所示:

new Vivus('hexx--0', {
  type: 'oneByOne',
  duration: 100
}, function() {

  new Vivus('hexx--1', {
    type: 'oneByOne',
    duration: 100,

  });

});

这可行,但是a)意味着svg在动画触发之前是可见的,并且b)在一个可能有任意数量的svg的循环中是混乱且不可维护的。

是否可以在 JavaScript 中增加 ID,并在循环中生成新的 Vivus?下面是一个复制问题的小提琴,请注意,在真实情况下,html 是通过 php 循环生成的。

//all animating at same time 

new Vivus('hexx--0', {
  type: 'oneByOne',
  duration: 100,

});

new Vivus('hexx--1', {
  type: 'oneByOne',
  duration: 100,

});

//attempt at staggering

new Vivus('hexx--2', {
  type: 'oneByOne',
  duration: 100
}, function() {

  new Vivus('hexx--3', {
    type: 'oneByOne',
    duration: 100,

  });

});
svg {
  stroke: #000000;
}
<body>
  <svg version="1.1" id="hexx--0" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
    <g>
      <path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
                     		C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
                     		c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
                     		c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
                     		c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
                     		l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
    </g>
  </svg>
  <svg version="1.1" id="hexx--1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
    <g>
      <path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
                     		C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
                     		c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
                     		c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
                     		c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
                     		l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
    </g>
  </svg>
  <svg version="1.1" id="hexx--2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
    <g>
      <path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
                     		C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
                     		c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
                     		c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
                     		c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
                     		l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
    </g>
  </svg>
  <svg version="1.1" id="hexx--3" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="236.275px" height="265.632px" viewBox="0 0 236.275 265.632" enable-background="new 0 0 236.275 265.632" xml:space="preserve">
    <g>
      <path fill="none" stroke-miterlimit="10" d="M118.046,1.007c-3.865,0-7.692,1.056-11.377,3.167
                     		C73.449,23.21,40.564,42.238,12.778,58.341c-7.8,4.52-11.758,11.436-11.763,20.553c-0.019,35.954-0.021,71.906,0.002,107.86
                     		c0.006,9.091,3.974,15.999,11.798,20.531c33.368,19.336,64.021,37.025,93.71,54.08c7.575,4.354,15.698,4.335,23.328-0.054
                     		c30.902-17.775,61.802-35.635,93.377-53.891c7.991-4.621,12.043-11.68,12.042-20.98l-0.001-38.245l0.002-30.855
                     		c0.003-12.791,0.006-25.582-0.009-38.373c-0.012-9.109-4.005-16.067-11.868-20.68c-6.162-3.615-12.562-7.301-18.208-10.551
                     		l-22.8-13.16c-17.397-10.045-34.793-20.09-52.204-30.108C126.174,2.161,122.089,1.007,118.046,1.007" />
    </g>
  </svg>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vivus/0.3.1/vivus.js"></script>

4

1 回答 1

0

我最终弄清楚了这一点,并为我的麻烦获得了风滚草徽章。

第 1 步: 在 JavaScript 中,使用 for 循环递增 id:

    //increment the ids
    var i;
    for (i = 0; i < 7; i++) {

        new Vivus('hexx--' + i, {
            type: 'scenario-sync'
        });
    }

我将类型更改为“场景同步”,这意味着您可以使用 html 数据属性设置持续时间和延迟。

第 2 步:在 PHP 中,我将数据属性添加到路径中,如下所示:

path data-delay="<?php echo $delay ?>" data-duration="<?php echo $duration ?>"

并相应地增加了 php 循环中的值。

于 2016-09-09T18:09:46.290 回答