RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 799491
Accepted
Air
Air
Asked:2020-03-17 15:58:51 +0000 UTC2020-03-17 15:58:51 +0000 UTC 2020-03-17 15:58:51 +0000 UTC

如何在 SVG 上实现阴影和 z-index 的动画?

  • 772

SVG如何用阴影和变化的z-index元素来实现类似的例子?

如需完整效果,请展开至全屏并单击以查看动画。

const shadows = document.getElementsByClassName("shadows")[0];
const shadowsChildren = shadows.children;
let count = 0;
let left = false;
shadows.addEventListener("click", function() {
  let interval = setInterval(function() {
    !left ? count++ : count--;
    shadowsChildren[left ? count : count - 1].style.zIndex = left ? count : shadowsChildren.length - count;
    shadowsChildren[left ? count : count - 1].className = !left ? 'span' : '';

    if (count === shadowsChildren.length) {
      left = true;
      clearInterval(interval);
    }

    if (count === 0) {
      left = false;
      clearInterval(interval);
    }
  }, 100);
});
* {
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  background: #272727;
}

.container {
  position: fixed;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: orange;
}

.shadows {
  cursor: pointer;
  background: orange;
  position: relative;
  text-shadow: -15px 5px 20px #303030;
  width: 80vw;
  color: orange;
  text-align: center;
  letter-spacing: -20px;
  transition: all 0.25s ease-out .2s;
  font-family: fantasy;
  font-size: 200px;
}

span {
  position: relative;
  margin-left: -10px;
  transition: all 0.25s ease-out;
}

span.span {
  text-shadow: 15px 5px 20px #303030;
  transition: all 0.25s ease-out;
}
<div id="wrapper">
  <div class="container">
    <div class="shadows">
      <span>C</span>
      <span>L</span>
      <span>I</span>
      <span>C</span>
      <span>K</span>
      <span>M</span>
      <span>E</span>
    </div>
  </div>
</div>

选项css:hover

* {
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  background: #272727;
}

.container {
  position: fixed;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: orange;
}

.shadows {
  cursor: pointer;
  background: orange;
  position: relative;
  text-shadow: -15px 5px 20px #303030;
  width: 80vw;
  color: orange;
  text-align: center;
  letter-spacing: -20px;
  transition: all 0.25s ease-out .2s;
  font-family: fantasy;
  font-size: 200px;
}

span {
  position: relative;
  margin-left: -18px;
  transition: all 0.25s ease-out;
}

.shadows:hover span {
  text-shadow: 15px 5px 20px #303030;
}

.shadows:hover span:nth-child(1) {
  z-index: 8;
}

.shadows:hover span:nth-child(2) {
  z-index: 7;
}

.shadows:hover span:nth-child(3) {
  z-index: 6;
}

.shadows:hover span:nth-child(4) {
  z-index: 5;
}

.shadows:hover span:nth-child(5) {
  z-index: 4;
}

.shadows:hover span:nth-child(6) {
  z-index: 3;
}

.shadows:hover span:nth-child(7) {
  z-index: 2;
}

.shadows:hover span:nth-child(8) {
  z-index: 1;
}
<div class="container">
  <div class="shadows">
    <span>C</span>
    <span>L</span>
    <span>I</span>
    <span>C</span>
    <span>K</span>
    <span>M</span>
    <span>E</span>
  </div>
</div>

javascript
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Alexandr_TT
    2020-03-17T23:25:49Z2020-03-17T23:25:49Z

    阴影动画

    动画是通过改变dx滤镜属性来实现的feOffset

    要执行的命令:

    <feOffset dy="3" dx="-14">
              <animate  attributeName="dx" values="-12;20;20;-12;-12" dur="3s" fill="freeze" repeatCount="indefinite" />
    

    * {
      margin: 0;
      padding: 0;
    }
    
    text {
      text-align: center;
      font-family: sans-serif;
      font-size: 200px;
      font-weight: 900;
      fill: #FFA500;
      -webkit-filter: url(#shadowLetter);
      filter: url(#shadowLetter);
    }
    <svg viewBox="0 0 1000 600" style="background:#FFA500;">
      <defs>
        <filter id="shadowLetter" x="-35%" y="-15%" width="200" height="200">									  
          <feGaussianBlur in="SourceAlpha" result="inShadow" stdDeviation="8"/>
          <feOffset dy="3" dx="-14">
         <!-- Анимация тени Повторяющиеся цифры в атрибуте values, создают паузы в крайних позициях  -->
         <animate
              attributeName="dx"
              values="-12;20;20;-12;-12"
              dur="3s"
              fill="freeze"
              repeatCount="indefinite"/> 
          </feOffset>
          <feMerge>
            <feMergeNode/>
            <feMergeNode in="SourceGraphic"/>
          </feMerge>         
        </filter>
      </defs>
      <g id="anText" fill-opacity="0.85" transform="translate(150 0)">
        <text x="-1%" y="50%">C</text>
        <text x="10%" y="50%">L</text>
        <text x="18%" y="50%">I</text> 
        <text x="22%" y="50%">C</text> 
        <text x="33%" y="50%">K</text> 
        <text x="45%" y="50%">M</text>
        <text x="61.5%" y="50%">E</text> 
      </g>
    
    </svg>

    • 11
  2. Alexandr_TT
    2020-03-17T19:39:10Z2020-03-17T19:39:10Z

    阴影是使用置换过滤器制作的 -feOffset和模糊 -feGaussianBlur
    为了获得将一个字母叠加在另一个字母上的效果,我必须使用过滤器为每个字母编写一个单独的命令:

    <text  x="-1%" y="50%" fill="#FFA500" filter="url(#shadowLetter)"  > C</text>
    

    不幸的是,该应用程序看起来有点不同FireFox。Chrome

    以下是申请的全文。

    * {
      margin: 0;
      padding: 0;
    }
    
    text {
      text-align: center;
      font-family: sans-serif;
      font-size: 200px;
      font-weight: 900;
      fill: #FFA500;
      -webkit-filter: url(#shadowLetter);
      filter: url(#shadowLetter);
    }
    <svg viewBox="0 0 1000 600" style="background:#FFA500;">
      <defs>
        <filter id="shadowLetter" x="-35%" y="-15%" width="200" height="200">									  
          <feGaussianBlur in="SourceAlpha" result="inShadow" stdDeviation="8" />
          <feOffset dy="3" dx="-14"/>
          <feMerge>
            <feMergeNode/>
            <feMergeNode in="SourceGraphic"/>
          </feMerge>         
        </filter>
      </defs>
      <g fill-opacity="0.85" transform="translate(150 0)" >
        <text  x="-1%" y="50%">C</text>   
        <text  x="10%" y="50%">L</text>
        <text  x="18%" y="50%">I</text> 
        <text  x="22%" y="50%">C</text> 
        <text  x="33%" y="50%">K</text> 
        <text  x="45%" y="50%">M</text>
        <text  x="61.5%" y="50%">E</text> 
      </g>
    </svg>

    • 9
  3. Best Answer
    Alexandr_TT
    2020-03-18T02:53:54Z2020-03-18T02:53:54Z

    复杂的运动动画

    此动画的基础是单个字母的移动以及创建阴影的过滤器。

    • 在第一阶段,所有来自初始位置的字母都被收集在单词的中间。
    • 在第二阶段,字母从中间散落到原来的位置

    字母的所有移动都是通过改变“X”属性值的命令来实现的,它指定了字母的横坐标:

    <text  x="-1%" y="50%">C 
         <animate id="anC"  attributeName="x" begin="0s;anBackE.end+1s" values="-1%;22%" dur="0.5s" fill="freeze" repeatCount="1" /> 
         <animate id="anBackC"  attributeName="x" begin="anBackM.end+0.25s" values="22%;-1%" dur="0.5s" fill="freeze" repeatCount="1" /> 
        </text> 
    

    为了不混淆不同动画的开始和结束顺序,有必要考虑一个命名唯一动画标识符的系统。

    • 在此示例中,每个字母的开始动画命令都包含该字母的名称。例如:begin="anC",begin="anL"
    • 对于将字母返回到其原始位置的动画:

      begin="anBackC",begin="anBackL"

    • 动画序列的逻辑是使用以下结构实现的: begin="anBackC.end+0.25s"换句话说,听起来像这样 - 字母“L”的返回动画将在字母“C”的返回动画结束后开始,在等于0.25s(秒) 的暂停之后

    在全屏片段模式下观看更有趣的内容

    * {
      margin: 0;
      padding: 0;
    }
    
    text {
      text-align: center;
      font-family: sans-serif;
      font-size: 200px;
      font-weight: 900;
      fill: #FFA500;
      -webkit-filter: url(#shadowLetter);
      filter: url(#shadowLetter);
    }
    <svg viewBox="0 0 1000 600" style="background:#FFA500;">
      <defs>
        <filter id="shadowLetter" x="-35%" y="-15%" width="200" height="200">									  
          <feGaussianBlur in="SourceAlpha" result="inShadow" stdDeviation="8" />
          <feOffset dy="3" dx="-14"/>
          <feMerge>
            <feMergeNode/>
            <feMergeNode in="SourceGraphic"/>
          </feMerge>         
        </filter>
      </defs>
      <g id="anText" fill-opacity="0.85" transform="translate(150 0)">
        <text x="-1%" y="50%">C 
          <animate id="anC" attributeName="x" begin="0s;anBackE.end+1s" values="-1%;22%" dur="0.5s" fill="freeze" repeatCount="1" /> 
          <animate id="anBackC" attributeName="x" begin="anBackM.end+0.25s" values="22%;-1%" dur="0.5s" fill="freeze" repeatCount="1" /> 
        </text>
        <text x="10%" y="50%">L
          <animate id="anL" attributeName="x" begin="anE.end+0.25s" values="10%;22%" dur="0.4s" fill="freeze" repeatCount="1" />  
          <animate id="anBackL" attributeName="x" begin="anBackK.end+0.5s" values="22%;10%" dur="0.4s" fill="freeze" repeatCount="1" /> 
        </text>
        <text x="18%" y="50%">I
          <animate id="anI" attributeName="x" begin="anM.end+0.25s" values="18%;22%" dur="0.25s" fill="freeze" repeatCount="1" />  
          <animate id="anBackI" attributeName="x" begin="anK.end+0.25s" values="22%;18%" dur="0.25s" fill="freeze" repeatCount="1" />
        </text>	
        <text x="22%" y="50%">C
          <animate id="anStart" attributeName="y" begin="0.2s" values="50%;25%;50%" dur="2s" fill="freeze" repeatCount="indefinite" />   
        </text>
        <text x="33%" y="50%">K
          <animate id="anK" attributeName="x" begin="anI.end+0.25s" values="33%;22%" dur="0.25s" fill="freeze" repeatCount="1"/> 
          <animate id="anBackK" attributeName="x" begin="anBackI.end+0.5s" values="22%;33%" dur="0.5s" fill="freeze" repeatCount="1"/>
        </text>	
        <text x="45%" y="50%">M 
          <animate id="anM" attributeName="x" begin="anL.end+0.25s" values="45%;22%" dur="0.4s" fill="freeze" repeatCount="1"/> 
          <animate id="anBackM" attributeName="x" begin="anBackL.end+0.25s" values="22%;45%" dur="0.4s" fill="freeze" repeatCount="1"/>
        </text>
        <text x="61.5%" y="50%">E
          <animate id="anE" attributeName="x" begin="anC.end+0.25s" values="61.5%;22%" dur="0.5s" fill="freeze" repeatCount="1"/> 
          <animate id="anBackE" attributeName="x" begin="anBackC.end+0.25s" values="22%;61.5%" dur="0.4s" fill="freeze" repeatCount="1"/>
        </text> 
      </g>
    </svg>

    • 8

相关问题

Sidebar

Stats

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

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +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