CSS 流动边框

前言

最近对 requestAnimationFrame 有点感兴趣。


正文

先贴个效果图:

再贴个代码:

'use client';

import { useEffect, useRef, useState } from 'react';
import styles from './styles.module.css';

export default function Effect() {
  const position = useRef({ x: 0, y: 50 });
  const done = useRef(false);
  const [background, setBackgrond] = useState('');

  function move() {
    const { x, y } = position.current;

    setBackgrond(
      `radial-gradient(20% 40% at ${x}% ${y}%, red 0%, transparent 100%)`
    );

    if (x >= 0 && x < 50 && y <= 50 && y > 0) {
      position.current.x++;
      position.current.y--;
    } else if (x >= 50 && x < 100 && y >= 0 && y < 50) {
      position.current.x++;
      position.current.y++;
    } else if (x <= 100 && x > 50 && y >= 50 && y < 100) {
      position.current.x--;
      position.current.y++;
    } else if (x <= 50 && x > 0 && y <= 100 && y > 50) {
      position.current.x--;
      position.current.y--;
    }

    if (!done.current) requestAnimationFrame(move);
  }

  useEffect(() => {
    requestAnimationFrame(move);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  return (
    <div className={styles.button}>
      <button>CLICK</button>
      <span style={{ background }}></span>
    </div>
  );
}
.button {
  position: relative;
  display: block;
  margin: 50px;
  padding: 2px;
  width: 107px;
  height: 44px;
  text-align: center;
  background: #ccc3;
  border-radius: 20px;
  overflow: hidden;
}

.button button {
  width: 100%;
  height: 100%;
  background: white;
  border-radius: 20px;
}

.button span {
  content: '';
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

ps:若有错误之处,烦请留言指正。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值