当前位置: 首页 > 知识库问答 >
问题:

动漫js动画问题

申屠森
2023-03-14

我正在尝试用anime.js动画一个SVG图标。

下面是我的代码:

null

const cartIcon = '.cart-icon';

const cartIconAnimation = anime({
  targets: `${cartIcon} path`,
  
  strokeDashoffset: {
    value: [anime.setDashoffset, 0],
    easing: 'easeInOutSine',
    duration: 1500,
    direction: 'alternate',
  },
  
  fill: {
    value: '#000',
    easing: 'linear',
    duration: 400,
    delay: 1500,
  },
  
  complete: function() {
    
   anime({
      targets: `${cartIcon} .arrow`,
      translateY: [-30, 0],
      duration: 700,
      easing: 'easeOutElastic',
      direction: 'alternate',
      loop: true
    });
    
  }
  
});

const cartReplay = document.querySelector('.cart-icon').nextElementSibling;

cartReplay.addEventListener('click', cartIconAnimation.restart);
css prettyprint-override">.icon {
  width: 100px;
  height: 100px;
  margin-top: 20px;
 }
 
 svg {
  width: 100%;
  height: 100%;
  overflow: visible;
 }
 
 button {
 margin-top: 20px;
 }
<script src="https://cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script>
<div class="cart-icon icon">
        <svg height="426pt" viewBox="-6 0 426 426.82755" width="426pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <path class="arrow" fill="#fff" stroke="#000" stroke-width="3" d="m207.859375 80.316406 35 30c.007813.007813.023437.015625.035156.023438.289063.246094.597657.464844.925781.660156.042969.027344.085938.054688.128907.078125.320312.179687.648437.335937.992187.464844l.160156.058593c.339844.121094.6875.210938 1.042969.277344l.15625.023438c.738281.128906 1.488281.128906 2.226563 0l.15625-.023438c.355468-.066406.703125-.15625 1.042968-.277344l.160157-.058593c.339843-.128907.671875-.285157.988281-.464844.046875-.023437.089844-.050781.132812-.078125.328126-.195312.636719-.414062.925782-.660156.011718-.007813.023437-.015625.035156-.023438l35-30c2.9375-2.519531 3.277344-6.9375.757812-9.871094-2.515624-2.9375-6.933593-3.277343-9.867187-.757812l-23.445313 20.09375v-82.78125c0-3.867188-3.132812-7-7-7-3.867187 0-7 3.132812-7 7v82.78125l-23.445312-20.09375c-2.933594-2.519531-7.351562-2.179688-9.871094.757812-2.515625 2.933594-2.175781 7.351563.761719 9.871094zm0 0" />
          <path class="cart" fill="#fff" stroke="#000" stroke-width="3" d="m407.414062 133.679688h-312l-3.277343-24.574219c-.007813-.054688-.015625-.109375-.023438-.167969-3.195312-20.703125-21.050781-35.957031-42-35.871094h-42.699219c-3.867187 0-7 3.132813-7 7 0 3.863282 3.132813 7 7 7h42.699219c14.019531-.058594 25.980469 10.128906 28.160157 23.980469l33.792968 253.425781c.007813.054688.015625.113282.023438.167969 3.195312 20.703125 21.050781 35.957031 42 35.875h5.875c2.578125 15.195313 15.742187 26.3125 31.152344 26.3125 15.414062 0 28.578124-11.117187 31.15625-26.3125h58.46875c2.707031 15.863281 16.902343 27.160156 32.96875 26.242187 16.070312-.921874 28.878906-13.765624 29.757812-29.835937s-10.457031-30.234375-26.328125-32.898437c-15.871094-2.667969-31.210937 7.015624-35.632813 22.492187h-60c-3.871093-13.574219-16.273437-22.933594-30.386718-22.933594-14.117188 0-26.519532 9.359375-30.390625 22.933594h-6.640625c-14.019532.058594-25.984375-10.128906-28.164063-23.980469l-10.183593-76.382812h218.40625c18.117187-.007813 34.242187-11.484375 40.179687-28.597656l39.703125-114.578126c.742188-2.140624.402344-4.507812-.910156-6.355468s-3.441406-2.945313-5.707032-2.941406zm-95.523437 243.90625c9.726563 0 17.613281 7.882812 17.613281 17.609374 0 9.726563-7.886718 17.613282-17.613281 17.613282s-17.609375-7.886719-17.609375-17.613282c.011719-9.722656 7.890625-17.597656 17.609375-17.609374zm-120.777344 0c9.726563 0 17.609375 7.882812 17.613281 17.609374 0 9.726563-7.886718 17.613282-17.613281 17.613282-9.726562 0-17.609375-7.886719-17.609375-17.613282.011719-9.722656 7.886719-17.597656 17.609375-17.609374zm169.988281-124.621094c-3.984374 11.480468-14.800781 19.179687-26.953124 19.183594h-220.273438l-16.601562-124.46875h300.300781zm0 0" />
          </svg>
</div>          

<button class="icon-block__replay" type="button"> Replay </button>

null

当动画工作加载-一切都是完美的。但当我点击“重启”按钮时,有两个问题:

1)图标的箭头部分必须在行和填充动画完成后才开始动画,但它与行/填充动画一起在动画开始时开始;

2)当填充动画完成时,箭头向顶部移动,我不知道该怎么修复它

所有的帮助将非常感谢!谢谢!

共有1个答案

轩辕实
2023-03-14

问题是,你没有暂停反弹动画,它开始时,中风动画结束。

将弹出动画加到一个变量中,以便在用户单击时暂停它。

// Declare the variable here to provide it with the right scope.
let bounceAnimation;

const cartIconAnimation = anime({
  // some options...

  complete: function() {
    bounceAnimation = anime({
      // your bounce options...
    });
  }
});

然后可以通过以下方式修改事件侦听器:

cartReplay.addEventListener('click', function() {
  bounceAnimation.pause()
  cartIconAnimation.restart()
});

请尝试下面的代码段。

null

const cartIcon = '.cart-icon';
let bounceAnimation;

const cartIconAnimation = anime({
  targets: `${cartIcon} path`,
  
  strokeDashoffset: {
    value: [anime.setDashoffset, 0],
    easing: 'easeInOutSine',
    duration: 1500,
    direction: 'alternate',
  },
  
  fill: {
    value: '#000',
    easing: 'linear',
    duration: 400,
    delay: 1500,
  },
  
  complete: function() {
    
   bounceAnimation = anime({
      targets: `${cartIcon} .arrow`,
      translateY: [-30, 0],
      duration: 700,
      easing: 'easeOutElastic',
      direction: 'alternate',
      loop: true
    });
    
  }
  
});

const cartReplay = document.querySelector('.cart-icon').nextElementSibling;

cartReplay.addEventListener('click', function() {
  bounceAnimation.pause()
  cartIconAnimation.restart()
});
.icon {
  width: 100px;
  height: 100px;
  margin-top: 20px;
 }
 
 svg {
  width: 100%;
  height: 100%;
  overflow: visible;
 }
 
 button {
 margin-top: 20px;
 }
<script src="https://cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script>
<div class="cart-icon icon">
        <svg height="426pt" viewBox="-6 0 426 426.82755" width="426pt" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <path class="arrow" fill="#fff" stroke="#000" stroke-width="3" d="m207.859375 80.316406 35 30c.007813.007813.023437.015625.035156.023438.289063.246094.597657.464844.925781.660156.042969.027344.085938.054688.128907.078125.320312.179687.648437.335937.992187.464844l.160156.058593c.339844.121094.6875.210938 1.042969.277344l.15625.023438c.738281.128906 1.488281.128906 2.226563 0l.15625-.023438c.355468-.066406.703125-.15625 1.042968-.277344l.160157-.058593c.339843-.128907.671875-.285157.988281-.464844.046875-.023437.089844-.050781.132812-.078125.328126-.195312.636719-.414062.925782-.660156.011718-.007813.023437-.015625.035156-.023438l35-30c2.9375-2.519531 3.277344-6.9375.757812-9.871094-2.515624-2.9375-6.933593-3.277343-9.867187-.757812l-23.445313 20.09375v-82.78125c0-3.867188-3.132812-7-7-7-3.867187 0-7 3.132812-7 7v82.78125l-23.445312-20.09375c-2.933594-2.519531-7.351562-2.179688-9.871094.757812-2.515625 2.933594-2.175781 7.351563.761719 9.871094zm0 0" />
          <path class="cart" fill="#fff" stroke="#000" stroke-width="3" d="m407.414062 133.679688h-312l-3.277343-24.574219c-.007813-.054688-.015625-.109375-.023438-.167969-3.195312-20.703125-21.050781-35.957031-42-35.871094h-42.699219c-3.867187 0-7 3.132813-7 7 0 3.863282 3.132813 7 7 7h42.699219c14.019531-.058594 25.980469 10.128906 28.160157 23.980469l33.792968 253.425781c.007813.054688.015625.113282.023438.167969 3.195312 20.703125 21.050781 35.957031 42 35.875h5.875c2.578125 15.195313 15.742187 26.3125 31.152344 26.3125 15.414062 0 28.578124-11.117187 31.15625-26.3125h58.46875c2.707031 15.863281 16.902343 27.160156 32.96875 26.242187 16.070312-.921874 28.878906-13.765624 29.757812-29.835937s-10.457031-30.234375-26.328125-32.898437c-15.871094-2.667969-31.210937 7.015624-35.632813 22.492187h-60c-3.871093-13.574219-16.273437-22.933594-30.386718-22.933594-14.117188 0-26.519532 9.359375-30.390625 22.933594h-6.640625c-14.019532.058594-25.984375-10.128906-28.164063-23.980469l-10.183593-76.382812h218.40625c18.117187-.007813 34.242187-11.484375 40.179687-28.597656l39.703125-114.578126c.742188-2.140624.402344-4.507812-.910156-6.355468s-3.441406-2.945313-5.707032-2.941406zm-95.523437 243.90625c9.726563 0 17.613281 7.882812 17.613281 17.609374 0 9.726563-7.886718 17.613282-17.613281 17.613282s-17.609375-7.886719-17.609375-17.613282c.011719-9.722656 7.890625-17.597656 17.609375-17.609374zm-120.777344 0c9.726563 0 17.609375 7.882812 17.613281 17.609374 0 9.726563-7.886718 17.613282-17.613281 17.613282-9.726562 0-17.609375-7.886719-17.609375-17.613282.011719-9.722656 7.886719-17.597656 17.609375-17.609374zm169.988281-124.621094c-3.984374 11.480468-14.800781 19.179687-26.953124 19.183594h-220.273438l-16.601562-124.46875h300.300781zm0 0" />
          </svg>
</div>          

<button class="icon-block__replay" type="button"> Replay </button>
 类似资料:
  • 现在我必须使用anime js制作一些动画:第一步是通过点击img(id='arrow')显示三个隐藏元素(input[type='date'],input[type='time'],select)。然后隐藏所有的em如果用户点击箭头另一次,切换:)但当页面加载动画自动启动,而不是通过点击。

  • 自动识别图像进行动漫化处理模式一 返回二进制文件流模式二 返回base64字符串模式三 通过图片url返回base64结果 动漫化API调用示例代码 github地址: https://github.com/picup-shop cURL Python PHP Java nodejs .net Objective-C curl -H 'APIKEY: INSERT_YOUR_API_KEY_HER

  • 一款手机阅读漫画应用,其中有下载、分享、收藏等功能。 [Code4App.com]

  • 在线漫画阅读App,下载并显示漫画。 作者说:自己本打算做个漫画赚点广告费,结果赚了个冰棍钱,最终决定分享给大家了吧,源码 接口数据(数百万数据开放),大家也可以用我开放的后台接口开发新的客户端,随意,哈哈。 [Code4App.com]

  • 本文向大家介绍关于js动画和css3动画的差异性?相关面试题,主要包含被问及关于js动画和css3动画的差异性?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 渲染线程分为main thread和compositor thread,如果css动画只改变transform和opacity,这时整个CSS动画得以在compositor trhead完成(而js动画则会在main thread执

  • 当我打开视图控制器时,我有一个从顶部飞出的视图。我已将UIView的Y约束设置为-200,当视图加载时,将调用以下内容,一切正常: 但是现在我有一个关闭按钮,它应该将动画回到-200位置,然后从屏幕上删除视图控制器。但是这个动画没有发生。视图控制器被直接删除。这是我正在做的: 我提到了这个链接。这似乎对他们有效,但对我无效。请帮忙。