【问题标题】:How to reasonably animate Font Awesome lock icons如何合理地为 Font Awesome 锁定图标设置动画
【发布时间】:2021-11-14 06:16:35
【问题描述】:

我想知道如何在以某种智能方式切换/动画它们的同时将Font Awesome Lock IconFont Awesome Lock Open Icon 交织在一起。例如有this flip animation

<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/js/all.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta2/css/all.min.css" rel="stylesheet"/>
<div class="fa-5x">
  <i class="fas fa-lock fa-flip" style="--fa-animation-duration: 10s; color: red"></i>
  <i class="fas fa-unlock fa-flip" style="--fa-animation-duration: 10s; color: green"></i>
</div>

例如,现在叠加两个图标是有意义的,将一个设置为display: none,然后在单击或悬停时切换它们。但是如何在动画时间的一半之后切换它们,以便在各个图标只能从侧面看到一条线的那一刻进行交换?或者您对合理的动画有更好的想法?

【问题讨论】:

  • 我假设你不需要它一直翻转,只是当事件发生时(用户点击或其他)你希望它翻转动画并更改图标。如果是这种情况,在您的代码中,当您想要更改图标时,您应该添加一个类 fa-flip 并在超时时将 fa-lock 更改为 fa-unlock
  • 是的,你是对的。这是个好主意。谢谢!

标签: javascript css font-awesome


【解决方案1】:

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

const icon = document.getElementById('icon');
icon.addEventListener('click', () => {
  icon.classList.toggle('green');
});

const icon2 = document.getElementById('icon2');
icon2.addEventListener('click', () => {
  icon2.classList.toggle('green');
})
*,
::after,
::before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: hsl(201, 27%, 10%);
}

body {
  height: 100vh;
  background: var(--bg);
  display: flex;
  justify-content: center;
  align-items:center;
  gap: 1em;
  position: relative;
}

#icon,
#icon2 {
  width: 52px;
  height: auto;
  position: relative;
  cursor: pointer;
}

#icon rect,
#icon path,
#icon2 path {
  transition: all 0.5s ease-in-out;
}

#icon rect {
  fill: red;
}
#icon2 path#body {
fill: red;
}

#icon path,
#icon2 path#hook {
  stroke: red;
  stroke-dasharray: 30;
  stroke-dashoffset: 5;
  fill: none;
}

#icon.green rect,
#icon2.green path#body {
  fill: green;
}

#icon.green path,
#icon2.green path#hook {
  stroke: green;
  stroke-dasharray: 20;
}

/* keyhole */

#keyhole {
  width: 10px;
  height: 16px;
  border-radius: 25px;
  position: absolute;
  top: 65%;
  left: 50%;
  background-color: var(--bg);
  transform: translate(-50%, -50%) rotate(0deg);
  transition: all 0.5s ease-in-out;
  z-index: 1;
}

#icon.green #keyhole {
  transform: translate(-50%, -50%) rotate(-180deg);
}
<div id="icon">
  <div id="keyhole"></div>
  <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>

<div id="icon2">
  <svg viewBox="0 0 22 25"  xmlns="http://www.w3.org/2000/svg">
    <path id="hook" d="M5.73633 10.4592V7.32508C5.73633 4.31064 8.18002 1.86694 11.1944 1.86694C14.2089 1.86694 16.6526 4.31064 16.6526 7.32508V10.4592" stroke-width="3.5"/>
    <path id="body" d="M3.50537 10.1519H18.8831C20.5399 10.1519 21.8831 11.4951 21.8831 13.1519V21.4387C21.8831 23.0956 20.5399 24.4387 18.8831 24.4387H3.50537C1.84852 24.4387 0.505371 23.0956 0.505371 21.4387V13.1519C0.505371 11.4951 1.84852 10.1519 3.50537 10.1519ZM11.1945 13.9858C10.0865 13.9858 9.18823 14.884 9.18823 15.9921V18.3328C9.18823 19.4408 10.0865 20.3391 11.1945 20.3391C12.3026 20.3391 13.2008 19.4408 13.2008 18.3328V15.9921C13.2008 14.884 12.3026 13.9858 11.1945 13.9858Z"/>
  </svg>
</div>

【讨论】:

  • 这太棒了! :)
  • 好吧,如果我只使用您解决方案中的解锁效果而不使用锁定效果?那么如果输入的密码错误,锁图标的动画将只是第二种状态,而不是第三种状态。 - 伟大的!谢谢! :)
  • 要创建钥匙孔并为其设置动画,请在id=#icon 内添加额外的div,position: absoluteborder-radiusbackground-color 必须与主背景颜色相同。当然,id=#icon 必须有 position: relative.
  • 更新了 sn-p。
  • 您可以将逻辑css functions 用于keyhole 或将静态px 单位替换为灵活的em, rem, %, vh, vw
猜你喜欢
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
相关资源
最近更新 更多