jQuery文字滚动切换动画特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery文字滚动切换动画特效</title>
    <style>
        body {
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            font-family: 'Arial', sans-serif;
            overflow: hidden;
            color: white;
        }
        
        .text-slider {
            width: 80%;
            max-width: 800px;
            height: 200px;
            position: relative;
            overflow: hidden;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }
        
        .slider-container {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 90%;
            text-align: center;
        }
        
        .slider-text {
            font-size: 2.5rem;
            font-weight: bold;
            line-height: 1.5;
            margin: 0;
            position: absolute;
            width: 100%;
            opacity: 0;
            transition: all 0.8s ease-in-out;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }
        
        .slider-text.active {
            opacity: 1;
            transform: translateY(0);
        }
        
        .slider-text.next {
            transform: translateY(50px);
        }
        
        .slider-text.prev {
            transform: translateY(-50px);
        }
        
        .controls {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            gap: 15px;
        }
        
        .control-btn {
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 1.2rem;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .control-btn:hover {
            background: rgba(255, 255, 255, 0.4);
            transform: scale(1.1);
        }
        
        /* 插入的链接样式 */
        .inserted-link {
            color: #a8edea;
            text-decoration: none;
            font-size: 14px;
            position: absolute;
            top: 20px;
            right: 20px;
            transition: all 0.3s ease;
            padding: 5px 10px;
            border-radius: 20px;
            background: rgba(0, 0, 0, 0.2);
        }
        
        .inserted-link:hover {
            color: #fff;
            background: rgba(0, 0, 0, 0.4);
            text-shadow: 0 0 10px rgba(168, 237, 234, 0.7);
        }
        
        @media (max-width: 768px) {
            .slider-text {
                font-size: 1.8rem;
            }
            
            .text-slider {
                height: 150px;
            }
        }
    </style>
</head>
<body>
    <div class="text-slider">
        <div class="slider-container">
            <p class="slider-text active">欢迎使用文字滚动特效</p>
            <p class="slider-text">轻松创建动态文字切换</p>
            <p class="slider-text">支持多种动画效果</p>
            <p class="slider-text">响应式设计适配各种设备</p>
            <p class="slider-text">点击按钮控制切换</p>
        </div>
        
        <div class="controls">
            <button class="control-btn prev-btn">←</button>
            <button class="control-btn next-btn">→</button>
        </div>
        
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            const texts = $('.slider-text');
            let currentIndex = 0;
            let isAnimating = false;
            
            // 初始化
            function initSlider() {
                texts.each(function(index) {
                    if (index !== currentIndex) {
                        $(this).removeClass('active prev next');
                    } else {
                        $(this).addClass('active');
                    }
                });
            }
            
            // 切换到下一个文本
            function nextText() {
                if (isAnimating) return;
                isAnimating = true;
                
                const nextIndex = (currentIndex + 1) % texts.length;
                
                texts.eq(currentIndex).removeClass('active').addClass('prev');
                texts.eq(nextIndex).addClass('next');
                
                setTimeout(() => {
                    texts.eq(currentIndex).removeClass('prev');
                    texts.eq(nextIndex).removeClass('next').addClass('active');
                    currentIndex = nextIndex;
                    isAnimating = false;
                }, 800);
            }
            
            // 切换到上一个文本
            function prevText() {
                if (isAnimating) return;
                isAnimating = true;
                
                const prevIndex = (currentIndex - 1 + texts.length) % texts.length;
                
                texts.eq(currentIndex).removeClass('active').addClass('next');
                texts.eq(prevIndex).addClass('prev');
                
                setTimeout(() => {
                    texts.eq(currentIndex).removeClass('next');
                    texts.eq(prevIndex).removeClass('prev').addClass('active');
                    currentIndex = prevIndex;
                    isAnimating = false;
                }, 800);
            }
            
            // 自动轮播
            let autoPlayInterval = setInterval(nextText, 3000);
            
            // 鼠标悬停时暂停自动轮播
            $('.text-slider').hover(
                function() {
                    clearInterval(autoPlayInterval);
                },
                function() {
                    autoPlayInterval = setInterval(nextText, 3000);
                }
            );
            
            // 按钮控制
            $('.next-btn').click(nextText);
            $('.prev-btn').click(prevText);
            
            // 键盘控制
            $(document).keydown(function(e) {
                if (e.keyCode === 37) { // 左箭头
                    prevText();
                } else if (e.keyCode === 39) { // 右箭头
                    nextText();
                }
            });
            
            initSlider();
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值