jquery+swiper组件实现时间轴滑动年份tab切换效果

yizhihongxing

下面是关于“jquery+swiper组件实现时间轴滑动年份tab切换效果”的完整攻略:

1. 准备工作

首先,我们需要引入jQuery和Swiper库,并且在HTML页面中创建好相关的DOM结构。例如,我们在页面中创建一个时间轴的整体容器(用一个div包含),并在其中放置一个swiper容器,再在swiper容器中创建每个年份的tab标签(用div包含,并设为inline-block布局)和tab内容(用div包含,每个tab内容设为swiper-slide类)。具体代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>时间轴滑动年份tab切换效果</title>
    <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
    <style>
        .timeline {
            position: relative;
            width: 100%;
            height: 400px;
            overflow: hidden;
        }
        .swiper-container {
            width: 100%;
            height: 100%;
        }
        .year-tab {
            display: inline-block;
            width: 80px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            font-size: 18px;
            color: #666;
            cursor: pointer;
        }
        .year-tab.active {
            color: #fff;
            background-color: #333;
        }
        .swiper-slide {
            height: 100%;
            box-sizing: border-box;
            padding: 20px;
            font-size: 16px;
            color: #333;
            background-color: #f5f5f5;
        }
        .swiper-slide p {
            margin: 0;
        }
    </style>
</head>
<body>
<div class="timeline">
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">
                <p>这是第一个年份的内容</p>
            </div>
            <div class="swiper-slide">
                <p>这是第二个年份的内容</p>
            </div>
            <div class="swiper-slide">
                <p>这是第三个年份的内容</p>
            </div>
            <!-- 这里可以根据需求添加更多的年份 -->
        </div>
    </div>
    <div class="timeline-year">
        <div class="year-tab active">2019</div>
        <div class="year-tab">2020</div>
        <div class="year-tab">2021</div>
        <!-- 这里也可以根据需求添加更多的年份 -->
    </div>
</div>
<script src="https://unpkg.com/jquery/dist/jquery.min.js"></script>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
</body>
</html>

2. 初始化Swiper实例

接下来,我们需要使用Swiper库创建一个swiper实例,使得我们可以在时间轴上滑动来切换年份的内容。创建swiper实例的过程较为简单,只需要在页面中添加以下代码:

var mySwiper = new Swiper('.swiper-container', {
    direction: 'horizontal',   // 垂直切换
    loop: true,    // 循环轮播
    slidesPerView: 'auto',   // 根据父容器自动调整宽度
});

3. 响应年份tab点击事件

为了实现年份tab的点击切换效果,我们需要添加一个点击事件监听器,监听每个年份tab的点击事件。在每次点击时,我们只需要切换当前tab的active类以及swiper实例的切换动画即可。代码如下:

var tabs = $('.year-tab');   // 获取所有年份tab
tabs.on('click', function () {
    // 切换tab的active类
    tabs.removeClass('active');
    $(this).addClass('active');
    // 切换swiper实例的轮播位置
    var index = $(this).index();
    mySwiper.slideToLoop(index, 500, false);
});

4. 细节优化

为了完善我们的时间轴滑动年份tab切换效果,我们还需要进行一些额外的细节优化,包括以下两点:

4.1 让当前年份tab始终处于可见区域

当我们在滑动时间轴时,当前的年份tab有可能被滑出了可见区域,这会影响我们的体验。为了解决这个问题,我们可以在每次swiper实例的切换动画结束后,判断当前选中的年份tab是否在可见区域,如果不在则将它滚动到可见区域即可。

// 给swiper实例添加一个回调函数,监听切换动画结束事件
mySwiper.on('transitionEnd', function () {
    // 判断当前选中的年份tab是否在可见区域
    var activeTab = $('.year-tab.active');
    if (activeTab.position().left + activeTab.outerWidth() > $(window).width()) {
        $('.timeline-year').animate({
            scrollLeft: activeTab.offset().left -$(window).width() / 2 + activeTab.outerWidth() / 2
        }, 500);
    } else if (activeTab.position().left < 0) {
        $('.timeline-year').animate({
            scrollLeft: activeTab.offset().left - $(window).width() / 2 - activeTab.outerWidth() / 2
        }, 500);
    }
});

4.2 优化移动端浏览体验

由于移动端设备的屏幕大小较小,我们需要对年份tab和swiper内容进行一些特殊处理,以优化移动端的浏览体验。例如,我们可以将年份tab的布局更改为上下布局,使得每个tab的高度占满整个swiper容器,这样就可以避免用户在移动端需要拖动才能将整个tab栏目完整展示出来的情况。同样地,我们也可以调整swiper-slide的样式,增加一些行距和字体大小,使得移动端用户可以更清晰地看到内容。

@media screen and (max-width: 768px) {
    .year-tab {
        display: block;
        width: 100%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 16px;
        color: #666;
        cursor: pointer;
    }
    .swiper-slide {
        padding: 10px 20px;
        font-size: 14px;
        line-height: 1.5em;
    }
}

到这里,我们就成功地实现了“jquery+swiper组件实现时间轴滑动年份tab切换效果”这个功能。完整的代码示例见下面。

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>时间轴滑动年份tab切换效果</title>
    <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
    <style>
        .timeline {
            position: relative;
            width: 100%;
            height: 400px;
            overflow: hidden;
        }
        .swiper-container {
            width: 100%;
            height: 100%;
        }
        .year-tab {
            display: inline-block;
            width: 80px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            font-size: 18px;
            color: #666;
            cursor: pointer;
        }
        .year-tab.active {
            color: #fff;
            background-color: #333;
        }
        .swiper-slide {
            height: 100%;
            box-sizing: border-box;
            padding: 20px;
            font-size: 16px;
            color: #333;
            background-color: #f5f5f5;
        }
        .swiper-slide p {
            margin: 0;
        }
        @media screen and (max-width: 768px) {
            .year-tab {
                display: block;
                width: 100%;
                height: 40px;
                line-height: 40px;
                text-align: center;
                font-size: 16px;
                color: #666;
                cursor: pointer;
            }
            .swiper-slide {
                padding: 10px 20px;
                font-size: 14px;
                line-height: 1.5em;
            }
        }
    </style>
</head>
<body>
<div class="timeline">
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">
                <p>这是第一个年份的内容</p>
            </div>
            <div class="swiper-slide">
                <p>这是第二个年份的内容</p>
            </div>
            <div class="swiper-slide">
                <p>这是第三个年份的内容</p>
            </div>
            <!-- 这里可以根据需求添加更多的年份 -->
        </div>
    </div>
    <div class="timeline-year">
        <div class="year-tab active">2019</div>
        <div class="year-tab">2020</div>
        <div class="year-tab">2021</div>
        <!-- 这里也可以根据需求添加更多的年份 -->
    </div>
</div>
<script src="https://unpkg.com/jquery/dist/jquery.min.js"></script>
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<script>
    $(function () {
        var mySwiper = new Swiper('.swiper-container', {
            direction: 'horizontal',   // 垂直切换
            loop: true,    // 循环轮播
            slidesPerView: 'auto',   // 根据父容器自动调整宽度
        });
        var tabs = $('.year-tab');   // 获取所有年份tab
        tabs.on('click', function () {
            // 切换tab的active类
            tabs.removeClass('active');
            $(this).addClass('active');
            // 切换swiper实例的轮播位置
            var index = $(this).index();
            mySwiper.slideToLoop(index, 500, false);
        });
        mySwiper.on('transitionEnd', function () {
            // 判断当前选中的年份tab是否在可见区域
            var activeTab = $('.year-tab.active');
            if (activeTab.position().left + activeTab.outerWidth() > $(window).width()) {
                $('.timeline-year').animate({
                    scrollLeft: activeTab.offset().left -$(window).width() / 2 + activeTab.outerWidth() / 2
                }, 500);
            } else if (activeTab.position().left < 0) {
                $('.timeline-year').animate({
                    scrollLeft: activeTab.offset().left - $(window).width() / 2 - activeTab.outerWidth() / 2
                }, 500);
            }
        });
    });
</script>
</body>
</html>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:jquery+swiper组件实现时间轴滑动年份tab切换效果 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • jQuery deferred.reject()方法

    jQuery的deferred.reject()方法用于在异步操作执行过程中,标记异步操作为失败状态,并触发相应的失败事件。本文将详细介绍deferred.reject()方法的语法和用法,并提供两个例说明。 语法 以下是deferred.reject()方法的基本语法: deferred.reject([args]); 在这个语法中,deferred是一个…

    jquery 2023年5月9日
    00
  • jQuery UI Datepicker altField选项

    以下是关于 jQuery UI Datepicker altField 选项的详细攻略: jQuery UI Datepicker altField 选项 altField 选项允许您指定一个表单字段,以便在用户选择日期时日期值存储在该字段中。这对于需要将日期值提交到服务器的表单非常有用。 语法 $(selectordatepicker({ altField…

    jquery 2023年5月11日
    00
  • jQuery UI菜单创建事件

    下面是jQuery UI菜单创建事件的详细攻略: 一、什么是jQuery UI菜单创建事件 jQuery UI是一个功能强大的jQuery插件集合,它提供了许多常用UI组件的实现,包括菜单组件。菜单组件支持多种类型的菜单,例如纵向菜单、横向菜单、嵌套菜单等。在菜单组件中,使用菜单创建事件,可以在菜单项初始化后,绑定事件处理函数。 二、使用方法 1. 语法 $…

    jquery 2023年5月12日
    00
  • jQuery Mobile Button Widget inline选项

    以下是使用jQuery Mobile Button Widget inline选项的完整攻略: 首先,需要在HTML文件中引入jQuery Mobile库。可以通过以下代码实现: <head> <meta charset="-"> <meta name="viewport" content…

    jquery 2023年5月11日
    00
  • jQuery 插件封装的方法

    当使用jQuery编写大型Web应用程序时,插件的使用可以使您的代码变得更加模块化、简单易懂。但是,在编写插件时,通常需要封装一些内部功能。 以下是封装jQuery插件的常见方法: 1. 使用 jQuery 的 extend() 在封装插件时,通常会将所有变量保存在一个单一的数据对象中,同时扩展jQuery对象的默认选项。这可以使用jQuery的extend…

    jquery 2023年5月27日
    00
  • jquery dataview数据视图插件使用方法

    jQuery dataview数据视图插件使用方法攻略 介绍 jQuery dataview(数据视图)插件是基于jQuery的插件,提供了一种简单易用的方法来展示数据。它可以帮助开发者快速构建各种数据视图,包括表格、卡片、列表等。此外,它还提供了许多可定制的选项,以满足开发者不同的需求。 安装 下载插件,并在页面中引入相关的JavaScript文件和CSS…

    jquery 2023年5月28日
    00
  • Ajax配合Spring实现文件上传功能代码

    为了实现Ajax配合Spring实现文件上传功能,我们需要按照以下步骤进行操作: 1.前端代码 首先,在前端页面代码中,我们需要创建一个上传文件的表单和一个用来显示上传进度的进度条,代码示例如下: <form id="upload" enctype="multipart/form-data"> <in…

    jquery 2023年5月27日
    00
  • jQWidgets jqxGrid columnsresize属性

    以下是关于“jQWidgets jqxGrid columnsresize属性”的完整攻略,包含两个示例说明: 简介 jqxGrid 控件的 columnsresize 属性用于启用或禁用表列的调整大小功能。 完整攻略 以下是 jqxGrid 控件 columnsresize 属性的完整攻略: 定义 resize 在 jqxGrid 控件中,可以使用 col…

    jquery 2023年5月11日
    00
合作推广
合作推广
分享本页
返回顶部