RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1165865
Accepted
Leks
Leks
Asked:2020-08-14 16:45:35 +0000 UTC2020-08-14 16:45:35 +0000 UTC 2020-08-14 16:45:35 +0000 UTC

为什么圆的 stroke-dashoffset 动画不起作用?

  • 772

<svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink"   
     width="200" height="200" viewBox="0 0 120 120" >  
      Фон серого цвета
 <rect width='100%' height='100%' fill='grey'/>
 <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
 <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)'  stroke-dashoffset='314' stroke-dasharray='314' stroke='dodgerblue' stroke-width='8'>
  <animate 
  attributeName='stroke-dashoffset'
  dur='4s'
  begin='svg1.click'
  values='0,314;314,0'
  fill='freeze'
  />
 </circle>
<text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
</svg> 

svg
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Stranger in the Q
    2020-08-14T17:11:58Z2020-08-14T17:11:58Z

    你试图动画stroke-dashoffset它是由一个数字设置的,并且values在属性中放入了成对的值,显然是从属性中复制的stroke-dasharray

    这样做:

    values='314;0'

    <svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"   
         width="200" height="200" viewBox="0 0 120 120" >  
          Фон серого цвета
     <rect width='100%' height='100%' fill='grey'/>
     <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
     <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)'  stroke-dashoffset='314' stroke-dasharray='314' stroke='dodgerblue' stroke-width='8'>
      <animate 
      attributeName='stroke-dashoffset'
      dur='4s'
      begin='svg1.click'
      values='314;0'
      fill='freeze'
      />
     </circle>
    <text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
    </svg>

    • 3
  2. Alexandr_TT
    2020-08-15T03:22:44Z2020-08-15T03:22:44Z
    1. Q 的答案中的@Strangerstroke-dashoffset
      使用 values 处 的属性来进行动画处理values="314;0",这对应于将行首的偏移量从最大值(314)更改为零。从而实现了从零画线到最大值(整圆)的动画

    2. 动画的时候stroke-dasharray,也用到了两个值,values="0, 314;314, 0
      动画的起始值是0, 314,其中0是破折号的长度,314是空格的长度,所以开头的线是不可见的。动画的
      最终值为314, 0,其中 314 是破折号的长度。所以这条线将被完全绘制

    因此,您的代码最初是正确的,但犯了一个错误:stroke-dashoffset您应该改为编写stroke-dasharray

    <svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"   
         width="200" height="200" viewBox="0 0 120 120" >  
          Фон серого цвета
     <rect width='100%' height='100%' fill='grey'/>
     <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
     <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)'  stroke-dashoffset='314' stroke-dasharray='314' stroke='dodgerblue' stroke-width='8'>
      <animate 
     attributeName='stroke-dasharray'
      dur='4s'
      begin='svg1.click'
      values='0,314;314,0'
      fill='freeze'
      />
     </circle>
    <text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
    </svg> 

    在我看来,有帮助的动画stroke-dasharray包含更多的可能性。

    例如,使用两对参数stroke-dasharray,您可以制作从一个点开始用两条单独的线填充圆的相同动画

    值="0, 157 0, 157; 0, 0, 314, 0"

    中风破折号=“0”

    <svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"   
         width="200" height="200" viewBox="0 0 120 120" >  
          Фон серого цвета
     <rect width='100%' height='100%' fill='grey'/>
     <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
     <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)'  stroke-dashoffset='0' stroke-dasharray='0, 157 0, 157' stroke='dodgerblue' stroke-width='8'>
      <animate 
     attributeName='stroke-dasharray'
      dur='4s'
      begin='svg1.click'
      values='0, 157 0, 157; 0, 0, 314, 0'
      fill='freeze'
      />
     </circle>
    <text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
    </svg>

    另一个选项
    用stroke-dashoffset="157"
    动画的起点偏移半圈

    <svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"   
         width="200" height="200" viewBox="0 0 120 120" >  
          Фон серого цвета
     <rect width='100%' height='100%' fill='grey'/>
     <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
     <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)' stroke-dasharray="0, 157 0, 157"  stroke-dashoffset="157"  stroke='dodgerblue' stroke-width='8'>
      <animate 
     attributeName='stroke-dasharray'
      dur='4s'
      begin='svg1.click'
      values='0, 157 0, 157; 0, 0, 314, 0'
      fill='freeze'
      />
     </circle>
    <text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
    </svg>

    通过更改stroke-dashoffset,您可以从圆上的任何点开始动画

    例如,当stroke-dashoffset="78.5"起点移动四分之一圆时314 / 4 = 78.5

    <svg id="svg1" xmlns="http://www.w3.org/2000/svg" 
        xmlns:xlink="http://www.w3.org/1999/xlink"   
         width="200" height="200" viewBox="0 0 120 120" >  
          Фон серого цвета
     <rect width='100%' height='100%' fill='grey'/>
     <circle r='50' cx='60' cy='60' fill='none' stroke='white' stroke-width='8' transform='rotate(-90 60 60)' />
     <circle r='50' cx='60' cy='60' fill='none' transform='rotate(-90 60 60)' stroke-dasharray="0, 157 0, 157"  stroke-dashoffset="78.5"  stroke='dodgerblue' stroke-width='8'>
      <animate 
     attributeName='stroke-dasharray'
      dur='4s'
      begin='svg1.click'
      values='0, 157 0, 157; 0, 0, 314, 0'
      fill='freeze'
      />
     </circle>
    <text x="50%" y="50%" text-anchor='middle' font-size='20' fill="">click</text> 
    </svg>

    • 2

相关问题

  • 字母S怎么画?

  • 在 svg 上绘制按钮

  • 将 viewBox 坐标转换为 svg 并将一个 svg 嵌入到另一个

  • 如何制作圆形箭头?

  • 如何消除 SVG 对象的大小限制不被模糊?

  • 为什么 SVG 不能在 iphone 上正确显示?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5