最近开发接到一个需求,前端展示付款的验证码,验证码时效 10 分钟,到期过期,同时在二维码的外侧有一个倒计时条,原本的实现方式是通过 JS 来控制,设置左上,左下,右上,右下四个矩形,每个矩形只显示一个折角的边框,从而模拟整个外框。

根据倒计时的时间轮询计算比例,分别控制四个矩形的宽高,从而实现倒计时的 CountDown 效果。这样的实现方式有几个问题:

  1. 使用4个元素来模拟,导致加入了很多不必要的数据
  2. js 轮询操作,代码很冗长。

本文主要介绍两种非 js 控制的矩形倒计时条的实现方法。

CSS 实现

css 实现方法的原理是:

  1. 设置四个background,使用linear-gradient 形成纯色的图片背景。
  2. 设置background的 size & position,使他们分布在元素的四周。
  3. 设置一个动画,均分成 4 个阶段,每个阶段将背景的位置按照顺时针平移。

具体可以看代码

  1. .progress {
  2. display: flex;
  3. justify-content: center;
  4. align-items: center;
  5. height: var(--height);
  6. width: var(--width);
  7. border-radius: calc(var(--line) / 2);
  8. background:
  9. linear-gradient(to right, var(--green) 99.99%, var(--blue))
  10. calc(-1 * var(--width)) 0rem
  11. / 100% var(--line),
  12. linear-gradient(to bottom, var(--green) 99.99%, var(--blue))
  13. calc(var(--width) - var(--line)) calc(-1 * var(--height))
  14. / var(--line) 100%,
  15. linear-gradient(to right, var(--green) 99.99%, var(--blue))
  16. var(--width) calc(var(--height) - var(--line))
  17. / 100% var(--line),
  18. linear-gradient(to top, var(--green), 99.99%, var(--blue))
  19. 0rem var(--height)
  20. / var(--line) 100%;
  21. background-repeat: no-repeat;
  22. animation: progress var(--time) linear forwards infinite;
  23. }
  24. @keyframes progress {
  25. 0% {
  26. background-position:
  27. calc(-1 * var(--width)) 0rem,
  28. calc(var(--width) - var(--line)) calc(-1 * var(--height)),
  29. var(--width) calc(var(--height) - var(--line)),
  30. 0rem var(--height);
  31. }
  32. 25% {
  33. background-position:
  34. 0rem 0rem,
  35. calc(var(--width) - var(--line)) calc(-1 * var(--height)),
  36. var(--width) calc(var(--height) - var(--line)),
  37. 0rem var(--height);
  38. }
  39. 50% {
  40. background-position:
  41. 0rem 0rem,
  42. calc(var(--width) - var(--line)) 0rem,
  43. var(--width) calc(var(--height) - var(--line)),
  44. 0rem var(--height);
  45. }
  46. 75% {
  47. background-position:
  48. 0rem 0rem,
  49. calc(var(--width) - var(--line)) 0rem,
  50. 0rem calc(var(--height) - var(--line)),
  51. 0rem var(--height);
  52. }
  53. 100% {
  54. background-position:
  55. 0rem 0rem,
  56. calc(var(--width) - var(--line)) 0rem,
  57. 0rem calc(var(--height) - var(--line)),
  58. 0rem 0rem;
  59. }
  60. }

SVG 实现

svg的实现则是hack了stroke-dasharray利用这个属性造出间断线来模拟倒计时,只要这个线足够长那么从视觉来看就是可以形成从全满变成全空的效果,这里的代码是这样的:

  1. <div class="father">
  2. <svg class="progressSvg" style={{'--speed': speed, '--progress': progress}} viewBox="0 0 120 120">
  3. <rect width="100" height="100" x="10" y="10" rx="10" ry="10" />
  4. </svg>
  5. <span class="son">{props.svg}</span>
  6. </div>

主要看rect部分,设置了一个圆角,所以矩形的起始位置设置成了x="10" y="10",并且由于设置了矩形的尺寸,为了能放下,所以 svg 标签的 viewBox="0 0 120 120" 从而放下这个圆角矩形。

这样以来,矩形的周长就是 400,所以设置stroke-dasharray 只要大于 400 即可,为了保险设置成 1000长度的实线,1000长度的虚线。

  1. .progressSvg rect {
  2. fill: none;
  3. stroke: blue;
  4. stroke-width: 4; // 控制边框的宽度
  5. /* stroke-linecap: round; */
  6. stroke-dasharray: 1000 1000;
  7. stroke-dashoffset: 0;
  8. animation: spin 60s infinite linear;
  9. }

接着就是让它动起来,这里使用的是控制stroke-offset来控制,就从0(完全是边框)转到 -400(旋转了所有的边框),因为实线的前面是虚线,只要开始设置负的 offset 那么就会是类似于被吃掉的效果。

  1. @keyframes spin {
  2. to {
  3. stroke-dashoffset: -400;
  4. }
  5. }

这样我们就实现了最简单的二维码倒计时进度条了。在线演示 codepen.io

组件化 基于 React

样式基本不需要修改,修改一下js 文件,主要通过 css 变量来对倒计时时间,进度进行控制。

这里根据需求:

  1. 页面在加载的时候会给出过期时间,例如总共支付时间10分钟的话,当进度条走了 60% 之后,进度条颜色变成红色。
  2. 根据给出的过期时间,页面刷新的时候,保持当前的进度。
  1. const CountedDown = (props) => {
  2. const [color, setColor] = React.useState("green");
  3. const [speed, setSpeed] = React.useState('100s');
  4. const [progress] = React.useState('0.75');
  5. return (
  6. <div>
  7. <div class="flex" style={{ "--bg": color, "--time": speed }}>
  8. <div class="countdown">
  9. <div class="progress">
  10. <div class="inner">
  11. {props.css}
  12. </div>
  13. </div>
  14. </div>
  15. </div>
  16. <div class="father">
  17. <svg class="progressSvg" style={{'--speed': speed, '--progress': progress}} viewBox="0 0 120 120">
  18. <rect width="100" height="100" x="10" y="10" rx="10" ry="10" />
  19. </svg>
  20. <span class="son">{props.svg}</span>
  21. </div>
  22. </div>
  23. );
  24. }

从上面的代码中,可以看出我们给 css 传入了 --bg 控制进度条的颜色,--time控制倒计时,读者可以自行查看在线演示代码。由于css版本的拐角存在问题,主要介绍svg版本。

在 svg 版本中, 传入了 --speed 控制速度,--progress控制进度,对应的 css :

  1. .progressSvg rect {
  2. fill: none;
  3. stroke: blue;
  4. stroke-width: 4;
  5. /* stroke-linecap: round; */
  6. stroke-dasharray: 1000 1000;
  7. - stroke-dashoffset: 0;
  8. - animation: spin 60s infinite linear;
  9. + stroke-dashoffset: calc((1 - var(--progress)) * (-400));
  10. + animation: spin var(--speed) infinite linear;
  11. }

--speed很好理解,主要解释--progress,上文中,我们知道使用动画就是让 stroke-offset按照逆时针旋转到 -400, 那么保存进度就是保存这个 offset 值,当我们认为现在的百分比进度是0.75的话,就需要提前 手动spin (1-0.75)*(-400)

可以用于生产的 React 组件 可以参考下面的代码:

  1. /* CountDown.module.css */
  2. .father {
  3. position: relative;
  4. }
  5. .son {
  6. position: absolute;
  7. top: 50%;
  8. left: 50%;
  9. transform: translate(-50%, -50%);
  10. width: 12rem;
  11. height: 100%;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. }
  16. .progress {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. @keyframes spin {
  21. to {
  22. stroke-dashoffset: -400;
  23. }
  24. }
  25. .progress rect {
  26. fill: none;
  27. stroke: var(--color);
  28. stroke-width: 4;
  29. /* stroke-linecap: round; */
  30. stroke-dasharray: 1000 1000;
  31. stroke-dashoffset: calc(-400 * var(--rate));
  32. animation: spin 600s infinite linear;
  33. /* animation-direction: alternate; */
  34. }
  1. import React from 'react';
  2. import styles from './CountDown.module.css';
  3. interface MyCSSProperties extends React.CSSProperties {
  4. '--color': string;
  5. '--rate': string;
  6. }
  7. const CountDown = ({
  8. color,
  9. timer,
  10. children,
  11. }: {
  12. color: string;
  13. timer: number;
  14. children: React.ReactNode;
  15. }) => {
  16. const style: MyCSSProperties = {
  17. // Add a CSS Custom Property
  18. '--color': color,
  19. '--rate': `${1 - timer / (600 * 1000)}`,
  20. };
  21. return (
  22. <div className={styles.father}>
  23. <svg className={styles.progress} viewBox="0 0 120 120">
  24. <rect style={style} width="100" height="100" x="10" y="10" rx="6" ry="6" />
  25. </svg>
  26. <span className={styles.son}>{children}</span>
  27. </div>
  28. );
  29. };
  30. export { CountDown };
  1. /* usage */
  2. import { useCountDown } from 'ahooks';
  3. import React, { useEffect, useState } from 'react';
  4. const Index = ()=>{
  5. const [barColor, setBarColor] = useState('blue'); // red
  6. const [expiryTimer, setTargetDate, formattedRes] = useCountDown({
  7. targetDate: dataRes.expiredAt,
  8. onEnd,
  9. });
  10. useEffect(() => {
  11. if (timer !== 0 && timer < 600 * 0.35 * 1000) {
  12. setBarColor('red');
  13. }
  14. }, [expiryTimer]);
  15. return (
  16. <CountDown color={barColor} timer={timer}>
  17. <div
  18. className={classNames({
  19. hidden: show,
  20. })}
  21. id="qrcode"
  22. ref={qrcodeRef}
  23. />
  24. </CountDown>
  25. )
  26. }