CSS+SVG实现简单的点赞效果

先看看效果图:
在这里插入图片描述

通过改变 svgstroke 属性实现点赞效果

<template>
  <div class="app-container">
    <label class="label" for="checkbox">
      <input id="checkbox" class="checkbox" type="checkbox" hidden />
      <svg
        t="1663813797165"
        class="icon"
        viewBox="0 0 1024 1024"
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        p-id="3350"
      >
        <path
          d="M667.786667 117.333333C832.864 117.333333 938.666667 249.706667 938.666667 427.861333c0 138.250667-125.098667 290.506667-371.573334 461.589334a96.768 96.768 0 0 1-110.186666 0C210.432 718.368 85.333333 566.112 85.333333 427.861333 85.333333 249.706667 191.136 117.333333 356.213333 117.333333c59.616 0 100.053333 20.832 155.786667 68.096C567.744 138.176 608.170667 117.333333 667.786667 117.333333z"
          p-id="3351"
          id="heart"
        ></path>
      </svg>
      <span class="spot"></span>
    </label>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  created() {},
  methods: {},
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  width: 100%;
  min-height: calc(100vh - 84px);
  align-items: center;
  justify-content: center;
  --color: red;
}
.label {
  cursor: pointer;
  svg {
    position: relative;
    width: 200px;
  }
  .spot {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 24px;
    height: 24px;
    transform: translate(-50%, -50%) scale(0);
    border-radius: 50%;
    background: var(--color);
    box-shadow: 0 -160px 0 0 var(--color), 0 160px 0 0 var(--color),
      -160px 0 0 0 var(--color), 160px 0 0 0 var(--color), -120px -120px 0 0 var(--color),
      120px -120px 0 0 var(--color), 120px 120px 0 0 var(--color),
      -120px 120px 0 0 var(--color);
  }
}

#heart {
  fill: #eee;
  // stroke: var(--color);
  /* 线条宽度 */
  stroke-width: 40;
  /* 设置线条为虚线,虚线的长度 */
  stroke-dasharray: 2600;
  /* 线条的位移 */
  stroke-dashoffset: 2600;
  /* 端点为圆头 */
  stroke-linecap: round;
}

/* 勾选复选框执行各个动画 */
.checkbox:checked + svg {
  animation: beat 1s linear forwards;
  #heart {
    animation: drawHeart 1s linear forwards;
  }
}
.checkbox:checked ~ .spot {
  animation: blink 0.5s 0.85s ease-in-out forwards;
}

/* 定义动画 */
/* 画心的动画 */
@keyframes drawHeart {
  0% {
    stroke: var(--color);
    stroke-dashoffset: 2600;
  }
  80% {
    // stroke: var(--color);
    fill: #eee;
    stroke-dashoffset: 0;
  }
  100% {
    stroke: var(--color);
    fill: var(--color);
    stroke-dashoffset: 0;
  }
}

/* 小圆点闪出的动画 */
@keyframes blink {
  0% {
    transform: translate(-50%, -50%) scale(0.5);
    opacity: 0.8;
  }
  50% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 0;
  }
}

/* 心跳动的动画 */
@keyframes beat {
  0% {
    transform: scale(1);
  }
  70% {
    transform: scale(1);
  }
  80% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡小多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值