【问题标题】:How to toggle Font Awesome animated icons?如何切换 Font Awesome 动画图标?
【发布时间】:2021-12-22 11:14:11
【问题描述】:

我接听this question

$('.lock').click(function() {
  $('.fa-lock', this).addClass('fa-flip');
  $('.fa-unlock', this).addClass('fa-flip');
  setTimeout(function() {
    $('.fa-lock').css('color', 'rgba(0, 0, 0, 0)');
    $('.fa-unlock').css('color', 'green');
  }, 600);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet" />
<style>
  @keyframes halfflip {
    to {
      transform: rotateY(360deg);
    }
  }
  
  .fa-flip {
    animation-name: halfflip;
    animation-timing-function: ease-in-out;
  }
  
</style>
<div class="fa-5x fa-stack lock">
  <i class="fas fa-unlock fa-stack-1x" style="--fa-animation-duration: 4s; --fa-animation-iteration-count: 1; color: transparent; animation-fill-mode: forwards; --fa-animation-delay:-2s"></i>
  <i class="fas fa-lock fa-stack-1x" style="--fa-animation-duration: 4s; --fa-animation-iteration-count: 1; color: red; animation-fill-mode: forwards; --fa-animation-delay:-2s"></i>
</div>

单击红色锁定图标会启动一个动画,该动画会导致绿色解锁图标。现在单击绿色解锁图标应该会执行相应的相反操作。如何切换两个图标?

【问题讨论】:

    标签: javascript css animation css-animations font-awesome


    【解决方案1】:

    您无法为两个不同的图标设置动画来获得真正的unlock effect。但是,如果不是很重要,您可以创建自己的 svg 图标和动画。

    解锁效果

    $('#icon').click(function() {
      $('#icon').toggleClass('green');
    });
    *,
    ::after,
    ::before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      height: 100vh;
      background-color: hsl(201, 27%, 10%);
      color: white;
      display: grid;
      place-items: center;
      position: relative;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet" />
    
    <style>
    #icon {
      width: 52px; /* adjust icon size  */
      height: auto;
      cursor: pointer;
    }
    #icon rect,
    #icon path {
      transition: all 0.5s ease-in-out;
    }
    
    #icon rect {
      fill: red;
    }
    #icon path {
      stroke: red;
      stroke-dasharray: 30;
      stroke-dashoffset: 5;
      fill: none;
    }
    
    #icon.green rect {
      fill: green;
    }
    #icon.green path {
      stroke: green;
      stroke-dasharray: 20;
    }
    </style>
    
    <div id="icon" class="fa-5x fa-stack lock">
      <svg viewBox="0 0 22 25">
        <rect x="0.505493" y="10.1519" width="21.3777" height="14.2868" rx="3"/>
        <path d="M5.73621 10.4592V7.32508C5.73621 4.31064 8.1799 1.86694 11.1943 1.86694V1.86694C14.2088 1.86694 16.6525 4.31064 16.6525 7.32508V10.4592"stroke-width="3.5" />
      </svg>
    </div>

    旋转效果

    $('#box').click(function() {
      $('#box').toggleClass('unlock');
    });
    *,
    ::after,
    ::before {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      height: 100vh;
      background-color: hsl(201, 27%, 10%);
      color: white;
      display: grid;
      place-items: center;
      position: relative;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet" />
    
    <style>
       :root {
        --duration-rotate: 1s;
        --delay: 1s;
      }
      
      #green,
      #red {
        backface-visibility: visible;
        transition: all var(--duration-rotate) ease-in-out;
      }
      
      #red {
        transition-delay: var(--delay);
      }
      
      #green {
        transform: rotateY(-270deg);
        transition-delay: 0s;
      }
      
      #box.unlock #red {
        transform: rotateY(90deg);
        backface-visibility: hidden;
        transition-delay: 0s;
      }
      
      #box.unlock #green {
        transform: rotateY(-360deg);
        backface-visibility: visible;
        transition-delay: var(--delay);
      }
    </style>
    
    <div id="box" class="fa-5x fa-stack lock">
      <i id="green" class="fas fa-unlock fa-stack-1x" style="color: green"></i>
      <i id="red" class="fas fa-lock fa-stack-1x" style="color: red"></i>
    </div>

    【讨论】:

    • 是的,我可以。你没看到我sn-p里的动画吗?您的动画非常好,除了 SVG 不干净并且括号区域有黑色背景。 “不挑剔”是什么意思?
    • 我看到了动画,但这是一个旋转变换,在两个图标之间切换,这不是解锁动画(这只是我的看法)。我的意思是 not critical 如果您只在项目中使用 Font Awesome 并且某些原因您可以使用其他图标。哦,我的错,我忘了添加到 path fill:none。 sn-p 更新了。
    • 其实你是对的。现在你的动画只是(字体)真棒。 :) 我很喜欢。非常感谢! (我想接受你的回答,但由于它不能完全回答我的问题,也许你可以添加一个适合我的 sn-p 的解决方案。即使你的解决方案要好得多。)(你的回答实际上适合我的@ 987654321@.) 不幸的是,我仍然不明白“不重要”这一点。
    • 没问题,我可以将此答案移至另一个您的问题以接受此问题。当你接受时,我可以在这里删除答案。将尝试解释简短。在某些项目中,只需要解决没有 javascript 的 css 和 html 问题。这是critical = important 用于项目出于某种原因,可能是后端或优化。 not critical = 你需要解决问题,无论如何,这都会解决。英语不是我的母语,正确地解释一下。
    • 更新了 sn-p。
    【解决方案2】:

    您可以调整 JS 以将其是否处于锁定状态保存在您检查以应用正确样式并在每次之后翻转它的变量中,像这样

    let locked = true;
    $('.lock').click(() => {
      $('.fa-lock', this).addClass('fa-flip');
      $('.fa-unlock', this).addClass('fa-flip');
      setTimeout(function() {
        if (locked) {
          $('.fa-lock').css('color', 'rgba(0, 0, 0, 0)');
          $('.fa-unlock').css('color', 'green');
        } else {
          $('.fa-lock').css('color', 'red');
          $('.fa-unlock').css('color', 'rgba(0, 0, 0, 0)');
        }
        locked = !locked;
      }, 100); // To change lock & unlock speed. 
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet" />
    <style>
      @keyframes halfflip {
        to {
          transform: rotateY(360deg);
        }
      }
      
      .fa-flip {
        animation-name: halfflip;
        animation-timing-function: ease-in-out;
      }
      
    </style>
    <div class="fa-5x fa-stack lock">
      <i class="fas fa-unlock fa-stack-1x" style="--fa-animation-duration: 4s; --fa-animation-iteration-count: 1; color: transparent; animation-fill-mode: forwards; --fa-animation-delay:-2s"></i>
      <i class="fas fa-lock fa-stack-1x" style="--fa-animation-duration: 4s; --fa-animation-iteration-count: 1; color: red; animation-fill-mode: forwards; --fa-animation-delay:-2s"></i>
    </div>

    【讨论】:

    • 谢谢!但是动画呢?
    • 抱歉,忘了动画部分。原始示例的动画实际上并不适合我。你有我可以运行的工作示例吗?
    • 动画成功了!?
    • 奇怪,以前在我的其他浏览器中无法使用。我相信如果您删除该类并重新添加它,它会再次制作动画。试试 $('.fa-lock', this).removeClass('fa-flip'); $('.fa-unlock', this).removeClass('fa-flip'); $('.fa-lock', this).addClass('fa-flip'); $('.fa-unlock', this).addClass('fa-flip');
    • 目前我在测试动画时遇到了麻烦,但这个其他答案可能会帮助您处理动画部分。 stackoverflow.com/questions/6268508/…
    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2014-12-27
    • 1970-01-01
    相关资源
    最近更新 更多