代码之家  ›  专栏  ›  技术社区  ›  Mengo

图标的SVG线交叉动画

  •  0
  • Mengo  · 技术社区  · 7 年前

    我有两个 icons ,一个在原始状态,一个在交叉状态。 我想动画的十字线,但改变 dash-offset 因为这是两个不同的SVG,所以没有用处。

    我在看 these 动画图标,但仍然无法找出神奇的部分。我想问:

    • 过渡工作流程应该是什么?
    • 我更喜欢使用纯css,有可能吗?

    请举一个有效的例子。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Paul LeBeau    7 年前

    该页面上的动画图标相当复杂(更需要imo)。但基本上他们所做的是从右向左滑动一个矩形遮罩,遮住第一个图标,揭开第二个图标。

    这是一个简化的版本,使用纯css,希望能让它更清晰。

    svg {
      width: 100px;
      height: 100px;
    }
    
    /* slides the two masks to the left on hover */
    svg:hover #bottom-rect,
    svg:hover #top-rect
    {
      transition: transform 500ms;
      transform: translate(-24px, 0px);
    }
    
    /* slides the two masks back to the right when not hovered */
    svg #bottom-rect,
    svg #top-rect {
      transition: transform 500ms;
      transform: translate(0px, 0px);
    }
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <defs>
            <mask id="bottom">
              <rect id="bottom-rect" width="24" height="24" fill="white" stroke="none"/>
            </mask>
            <mask id="top">
              <rect id="top-rect" x="24" width="24" height="24" fill="white" stroke="none"/>
            </mask>
        </defs>    
       
        <g mask="url(#bottom)">
          <path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path>
          <path d="M19 10v2a7 7 0 0 1-14 0v-2"></path>
          <line x1="12" y1="19" x2="12" y2="23"></line>
          <line x1="8" y1="23" x2="16" y2="23"></line>
        </g>
        <g mask="url(#top)">
          <line x1="1" y1="1" x2="23" y2="23"></line>
          <path d="M9 9v3a3 3 0 0 0 5.12 2.12M15 9.34V4a3 3 0 0 0-5.94-.6"></path>
          <path d="M17 16.95A7 7 0 0 1 5 12v-2m14 0v2a7 7 0 0 1-.11 1.23"></path>
          <line x1="12" y1="19" x2="12" y2="23"></line>
          <line x1="8" y1="23" x2="16" y2="23"></line>
        </g>
    </svg>
        2
  •  1
  •   sol    7 年前

    使用css的方法。

    只需在悬停时显示/隐藏所需的图标。要设置线条动画,请使用css动画 stroke-dasharray stroke-dashoffset .代码中的注释。

    /* for demo */
    svg {
      width: 100px;
      height: 100px;
      border: 1px solid black;
    } 
    
    /* hide the off icon */
    svg .off {
      opacity: 0;
    }
    
    /* when user hovers SVG, the on icon is hidden... */
    svg:hover .on {
      opacity: 0;
    }
    
    /* ... and the off icon is shown */
    svg:hover .off {
      opacity: 1;
    }
    
    /* initial values for the stroke
       -- these can be obtained with JS, 
       -- or you can work them out manually for CSS
    */
    
    svg .line {
      stroke-dashoffset: 40px;
      stroke-dasharray: 40px;
    }
    
    /* when user hovers SVG, animate the line */
    svg:hover .line {
      animation: addLine 800ms forwards;
    }
    
    /* values for line animation */
    @keyframes addLine {
      from {
        stroke-dashoffset: 40px;
      }
      to {
        stroke-dashoffset: 0;
      }
    }
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> 
        <g class="on">
          <path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path>
          <path d="M19 10v2a7 7 0 0 1-14 0v-2"></path>
          <line x1="12" y1="19" x2="12" y2="23"></line>
          <line x1="8" y1="23" x2="16" y2="23"></line>
        </g>
        <g class="off">
          <line class="line" x1="1" y1="1" x2="23" y2="23"></line>
          <path d="M9 9v3a3 3 0 0 0 5.12 2.12M15 9.34V4a3 3 0 0 0-5.94-.6"></path>
          <path d="M17 16.95A7 7 0 0 1 5 12v-2m14 0v2a7 7 0 0 1-.11 1.23"></path>
          <line x1="12" y1="19" x2="12" y2="23"></line>
          <line x1="8" y1="23" x2="16" y2="23"></line>
        </g>
    </svg>