当前位置: 首页 > news >正文

前端按钮动画

效果示例
在这里插入图片描述

代码示例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按钮点击动画1</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Pacifico&display=swap');

        html,
        body {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #1f1f1f;
            font-family: 'Pacifico', serif;
            overflow: hidden;
        }

        span {
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-grow: 1;
        }

        span::after {
            content: attr(data-text);
            position: absolute;
            background-color: #FC4F4F;
            border-radius: 10px;
            padding: 6px 20px;
            font-size: 22px;
            cursor: pointer;
            color: #fff;
            user-select: none;
            transition: transform 100ms ease-in;
        }

        span:active::after {
            transform: scale(0.9);
        }



        @keyframes pumping {
            50% {
                transform: scale(0.95);
            }
        }

        .shape {
            --size: 8px;
            position: absolute;
            top: calc(50% - var(--size));
            left: calc(50% - var(--size));
            width: calc(var(--size) * 2);
            height: calc(var(--size) * 2);
            animation: popup var(--d) cubic-bezier(.08, .56, .53, .98) forwards;
        }


        .heart {
            --size: 6px;
            background-color: var(--c);
        }

        .heart::before,
        .heart::after {
            content: "";
            position: absolute;
            width: 100%;
            height: 100%;
            background-color: var(--c);
            border-radius: 50%;
        }

        .heart::before {
            left: -50%;
        }

        .heart::after {
            top: -50%;
        }

        @keyframes popup {
            0% {
                opacity: 0;
            }

            60% {
                opacity: 1;
            }

            100% {
                opacity: 0;
                transform: translate(var(--x), var(--y)) rotate(var(--r));
            }
        }
    </style>
</head>

<body>
    <span data-text="Click Me"></span>
    <script>
        const colors = ['#FC4F4F', '#FFBC80', '#FF9F45', '#F76E11']
        // const shapes = ['square', 'circle', 'triangle', 'heart']
        const shapes = ['heart']

        const randomIntBetween = (min, max) => {
            return Math.floor(Math.random() * (max - min + 1) + min)
        }

        class Particle {
            constructor({ x, y, rotation, shape, color, size, duration, parent }) {
                this.x = x
                this.y = y
                this.parent = parent
                this.rotation = rotation
                this.shape = shape
                this.color = color
                this.size = size
                this.duration = duration
                this.children = document.createElement('div')
            }

            draw() {
                this.children.style.setProperty('--x', this.x + 'px')
                this.children.style.setProperty('--y', this.y + 'px')
                this.children.style.setProperty('--r', this.rotation + 'deg')
                this.children.style.setProperty('--c', this.color)
                this.children.style.setProperty('--size', this.size + 'px')
                this.children.style.setProperty('--d', this.duration + 'ms')
                this.children.className = `shape ${this.shape}`
                this.parent.append(this.children)
            }

            animate() {
                this.draw()

                const timer = setTimeout(() => {
                    this.parent.removeChild(this.children)
                    clearTimeout(timer)
                }, this.duration)
            }
        }

        function animateParticles({ total }) {
            for (let i = 0; i < total; i++) {
                const particle = new Particle({
                    x: randomIntBetween(-200, 200),
                    y: randomIntBetween(-100, -300),
                    rotation: randomIntBetween(-360 * 5, 360 * 5),
                    shape: shapes[randomIntBetween(0, shapes.length - 1)],
                    color: colors[randomIntBetween(0, colors.length - 1)],
                    size: randomIntBetween(4, 7),
                    duration: randomIntBetween(400, 800),
                    parent
                })
                particle.animate()
            }
        }

        const parent = document.querySelector('span')
        parent.addEventListener("touchstart", () => { }, false);
        parent.addEventListener('click', e => animateParticles({ total: 40 })
        )
    </script>
</body>

</html>

相关文章:

  • npm install的-S和-D的区别
  • iOS中卡顿产生的主要原因及优化思路
  • Apache Paimon Append Queue表解析
  • 【语音识别】- 几个主流模型
  • 数据库的介绍、分类、作用和特点
  • 【C++精简版回顾】14.(重载2)流重载
  • 【Python】python离线安装依赖
  • 3D工业相机及品牌集合
  • 蓝月亮,蓝禾,三七互娱,顺丰,康冠科技,金证科技24春招内推
  • git入门
  • PCIE Order Set
  • java spring cloud 企业电子招标采购系统源码:营造全面规范安全的电子招投标环境,促进招投标市场健康可持续发展
  • 大宇、固特、希亦超声波清洗机实测,哪款清洗效果好?一篇掌握
  • Laravel Octane 和 Swoole 协程的使用分析二
  • Unity 向量计算、欧拉角与四元数转换、输出文本、告警、错误、修改时间、定时器、路径、
  • SQL server创建数据库
  • leetcode--接雨水(双指针法,动态规划,单调栈)
  • 【AI Agent系列】【MetaGPT多智能体学习】3. 开发一个简单的多智能体系统,兼看MetaGPT多智能体运行机制
  • python66-Python的循环之常用工具函数
  • pyspark(一) DataFrame结合jupyter入门
  • 天津外国语大学原校长修刚突发疾病去世,享年68岁
  • “一对一讨论诸多事宜”,泽连斯基披露此次特泽会更多细节
  • 巴黎奥运后红土首秀落败,郑钦文止步马德里站次轮
  • 中国与肯尼亚签署共同发展经济伙伴关系框架协定
  • 历史一刻,两个航天员乘组拍摄全家福
  • 84%白化!全球珊瑚正经历最严重最大范围白化现象