Ripplet.js点击波插件特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ripplet.js点击波插件特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #1a1a2e, #16213e);
            font-family: 'Arial', sans-serif;
        }

        .container {
            width: 100%;
            max-width: 800px;
            padding: 20px;
            text-align: center;
        }

        h1 {
            color: white;
            margin-bottom: 30px;
            text-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
        }

        .btn {
            display: inline-block;
            padding: 12px 24px;
            margin: 10px;
            background: linear-gradient(45deg, #4CAF50, #8BC34A);
            color: white;
            border: none;
            border-radius: 50px;
            font-size: 16px;
            cursor: pointer;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }

        .btn:active {
            transform: translateY(1px);
        }

        .card {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            padding: 30px;
            margin: 20px 0;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
        }

        .card h2 {
            color: white;
            margin-bottom: 15px;
        }

        .card p {
            color: rgba(255, 255, 255, 0.7);
            line-height: 1.6;
        }

        .credit {
            margin-top: 40px;
            color: rgba(255, 255, 255, 0.7);
            font-size: 14px;
        }

        .credit a {
            color: #4CAF50;
            text-decoration: none;
            transition: color 0.3s;
        }

        .credit a:hover {
            color: #FFD700;
        }

        /* 自定义波纹效果 */
        .ripplet {
            position: absolute;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.4);
            transform: scale(0);
            animation: ripplet 0.6s linear;
            pointer-events: none;
        }

        @keyframes ripplet {
            100% {
                transform: scale(2);
                opacity: 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Ripplet.js点击波特效演示</h1>
        
        <button class="btn" id="btn1">点击我</button>
        <button class="btn" id="btn2">另一个按钮</button>
        <button class="btn" id="btn3">更多波纹</button>
        
        <div class="card" id="card1">
            <h2>卡片标题</h2>
            <p>点击卡片任意位置查看波纹效果。这是一个使用纯JavaScript实现的轻量级波纹效果插件。</p>
        </div>
        
        <div class="card" id="card2">
            <h2>另一个卡片</h2>
            <p>尝试点击这个卡片的不同位置,看看波纹效果如何从点击位置扩散开来。</p>
        </div>
        
        <div class="credit">
        </div>
    </div>

    <script>
        // Ripplet.js 实现
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化Ripplet.js
            function Ripplet(element, options) {
                this.element = typeof element === 'string' ? document.querySelector(element) : element;
                this.options = Object.assign({
                    color: 'rgba(255, 255, 255, 0.4)',
                    type: 'cover', // 'cover' or 'center'
                    duration: 600,
                    maxScale: 2,
                    easing: 'linear'
                }, options);
                
                this.init();
            }
            
            Ripplet.prototype.init = function() {
                this.element.style.position = 'relative';
                this.element.style.overflow = 'hidden';
                
                this.element.addEventListener('click', this.createRipple.bind(this));
            };
            
            Ripplet.prototype.createRipple = function(e) {
                const rect = this.element.getBoundingClientRect();
                let x, y;
                
                if (this.options.type === 'center') {
                    x = rect.width / 2;
                    y = rect.height / 2;
                } else {
                    x = e.clientX - rect.left;
                    y = e.clientY - rect.top;
                }
                
                const ripple = document.createElement('span');
                ripple.className = 'ripplet';
                
                ripple.style.width = ripple.style.height = Math.max(rect.width, rect.height) + 'px';
                ripple.style.left = (x - ripple.offsetWidth / 2) + 'px';
                ripple.style.top = (y - ripple.offsetHeight / 2) + 'px';
                ripple.style.backgroundColor = this.options.color;
                ripple.style.animation = `ripplet ${this.options.duration}ms ${this.options.easing}`;
                
                this.element.appendChild(ripple);
                
                // 动画结束后移除元素
                setTimeout(() => {
                    ripple.remove();
                }, this.options.duration);
            };
            
            // 初始化所有按钮和卡片
            new Ripplet('#btn1', { color: 'rgba(255, 255, 255, 0.6)', maxScale: 2.5 });
            new Ripplet('#btn2', { color: 'rgba(76, 175, 80, 0.6)', type: 'center' });
            new Ripplet('#btn3', { color: 'rgba(255, 193, 7, 0.6)', duration: 800 });
            new Ripplet('#card1', { color: 'rgba(255, 255, 255, 0.3)', maxScale: 3 });
            new Ripplet('#card2', { color: 'rgba(233, 30, 99, 0.3)', duration: 1000 });
            
            // 为整个body添加点击效果(可选)
            new Ripplet('body', { 
                color: 'rgba(255, 255, 255, 0.1)', 
                maxScale: 5, 
                duration: 1500 
            });
            
            // 动态改变颜色按钮
            document.getElementById('btn3').addEventListener('click', function() {
                const colors = [
                    'rgba(255, 87, 34, 0.6)',
                    'rgba(156, 39, 176, 0.6)',
                    'rgba(63, 81, 181, 0.6)',
                    'rgba(0, 150, 136, 0.6)'
                ];
                const randomColor = colors[Math.floor(Math.random() * colors.length)];
                new Ripplet(this, { color: randomColor, duration: 800 });
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值