jQuery图片切换和Tab切换示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery图片切换和Tab切换示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        
        /* 图片切换样式 */
        .image-switcher {
            margin-bottom: 30px;
        }
        .image-switcher-container {
            position: relative;
            height: 400px;
            overflow: hidden;
            border: 1px solid #ddd;
            margin-bottom: 10px;
        }
        .image-switcher img {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
            opacity: 0;
            transition: opacity 0.5s ease;
        }
        .image-switcher-controls {
            display: flex;
            justify-content: center;
            gap: 10px;
        }
        .image-switcher-btn {
            padding: 8px 15px;
            background: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.3s;
        }
        .image-switcher-btn:hover {
            background: #2980b9;
        }
        
        /* Tab切换样式 */
        .tab-container {
            margin-top: 30px;
        }
        .tab-header {
            display: flex;
            border-bottom: 1px solid #ddd;
        }
        .tab-btn {
            padding: 10px 20px;
            background: #f1f1f1;
            border: none;
            cursor: pointer;
            transition: all 0.3s;
        }
        .tab-btn.active {
            background: #3498db;
            color: white;
        }
        .tab-content {
            display: none;
            padding: 20px;
            border: 1px solid #ddd;
            border-top: none;
        }
        .tab-content.active {
            display: block;
        }
        
        .footer {
            margin-top: 30px;
            text-align: center;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>jQuery切换效果演示</h1>
        
        <!-- 图片切换部分 -->
        <div class="image-switcher">
            <h2>图片切换展示</h2>
            <div class="image-switcher-container">
                <img src="https://picsum.photos/id/237/800/400" alt="图片2">
                <img src="https://picsum.photos/id/238/800/400" alt="图片3">
            </div>
            <div class="image-switcher-controls">
                <button class="image-switcher-btn" data-index="0">图片1</button>
                <button class="image-switcher-btn" data-index="1">图片2</button>
                <button class="image-switcher-btn" data-index="2">图片3</button>
            </div>
        </div>
        
        <!-- Tab切换部分 -->
        <div class="tab-container">
            <h2>Tab切换展示</h2>
            <div class="tab-header">
                <button class="tab-btn active" data-tab="tab1">选项卡1</button>
                <button class="tab-btn" data-tab="tab2">选项卡2</button>
                <button class="tab-btn" data-tab="tab3">选项卡3</button>
            </div>
            <div class="tab-content active" id="tab1">
                <h3>选项卡1内容</h3>
                <p>这是第一个选项卡的内容。你可以在这里放置任何HTML内容。</p>
            </div>
            <div class="tab-content" id="tab2">
                <h3>选项卡2内容</h3>
                <p>这是第二个选项卡的内容。Tab切换是非常常见的网页交互效果。</p>
            </div>
            <div class="tab-content" id="tab3">
                <h3>选项卡3内容</h3>
                <p>这是第三个选项卡的内容。jQuery让这些交互效果变得非常简单。</p>
            </div>
        </div>
        
        <div class="footer">
            <p>© 2023 切换效果演示页面</p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // 图片切换功能
            $('.image-switcher-btn').on('click', function() {
                var index = $(this).data('index');
                $('.image-switcher img').removeClass('active').eq(index).addClass('active');
                $('.image-switcher-btn').removeClass('active');
                $(this).addClass('active');
            });
            
            // Tab切换功能
            $('.tab-btn').on('click', function() {
                var tabId = $(this).data('tab');
                $('.tab-btn').removeClass('active');
                $(this).addClass('active');
                $('.tab-content').removeClass('active');
                $('#' + tabId).addClass('active');
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值