Vue实现tab选项卡及内容滑动切换

本文介绍如何使用Vue.js不依赖外部插件,原生开发一个选项卡滑动切换功能,同时内容区域也跟随滑动。通过监听触摸事件,实现了平滑的滑动体验。示例代码包括HTML结构、CSS样式和JavaScript逻辑,详细展示了从界面到交互的完整实现过程。项目源码已上传至Gitee,可供参考和下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vue实现tab选项卡及内容滑动切换

近期公司要求给产品应用添加详情和评论的展示模块,为了达到用户体验效果,采取切换tab选项卡,联级滑动响应的内容进行展示,并且滑动内容切换tab选项卡。考虑到性能及兼容性问题,放弃采用Swiper等插件,直接使用Vue来进行原生开发。下面上代码。

效果图:
在这里插入图片描述

HTML:

<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>tab滑动选项卡</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<meta name="format-detection" content="telephone=no" />
	<!-- ui-base.css -->
	<link rel="stylesheet" href="style/ui-base.css" />
	<!-- ui-common.css -->
	<link rel="stylesheet" href="style/ui-common.css" />
</head>
<body>
	<div id="view" class="view i-view col ft-30" v-cloak>
        <!-- nav -->
        <div class="tab-box bc-fff">
            <div class="tab-item-box overflow-x">
                <a :class="['tab-item','ft-28','t-center', cur == index ? 'active' : '']" href="javascript:;" v-for="(item, index) in tabList" :key="index" @click="changeTab(index)">{{item}}</a>
                <div class="bottom-line"></div>
            </div>
        </div>
        <!-- 内容 -->
		<div class="content">
            <div class="content-box" @touchStart="touchStart" @touchmove="touchmove" @touchend="touchend">
                <div class="content-item" v-for="(item, index) in dataList" :key="index">{{item}}</div>
            </div>
        </div>
	</div>
	<script type="text/javascript" src="script/vuejs/vue.min.js"></script>
</body>
</html>

CSS:

       .tab-box{
            width: 100%;
            height: 1rem;
        }
        .tab-item-box{
            width: 100%;
            height: 100%;
            white-space: nowrap;
            position: relative;
        }
        .tab-item-box::-webkit-scrollbar { 
            width: 0 ;  
            height: 0; 
        }
        .tab-item-box{ 
            -ms-overflow-style: none; 
            overflow: -moz-scrollbars-none;
        }
        .bottom-line{
            position: absolute;
            width: .5rem;
            height: .08rem;
            background-color: #147ae2;
            bottom: 0;
            left: 0;
            margin-left: 0.5rem;
            border-radius: .1rem;
            transition: left .5s;
        }
        .tab-item{
            display: inline-block;
            width: 1.5rem;
            line-height: 1rem;
        }
        a.active{
            color: #147ae2;
        }
        .content{
            width: 100%;
            height: 100%;
            position: relative;
            overflow: hidden;
        }
        .content-box{
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            transition: left .5s;
        }
        .content-item{
            display: inline-block;
            width: 7.5rem;
            height: 100%;
            float: left;
        }
   

JS:

	<script type="text/javascript">
		let vm = new Vue({
			el: '#view',
			data: {
                cur:null,
                tabList:[1,2,3,4,5,6],
                dataList:[1,2,3,4,5,6],
                startX:null,
                moveX:null,
                index:0
            },
            mounted() {
                var itemWidth = document.querySelector(".content-item").offsetWidth;
                document.querySelector(".content-box").style.width = itemWidth * this.dataList.length + 'px';
            },
            methods: {
                changeTab(index) {
                    this.cur = index;
                    this.setMove(index);
                },
                /**
                 * 设置偏移
                 */
                setMove(index){
                    var itemWidth = document.querySelector(".content-item").offsetWidth;
                    var bottomLineWidth = document.querySelector(".tab-item").offsetWidth;
                    document.querySelector(".bottom-line").style.left = (bottomLineWidth *(index)) + 'px';
                    document.querySelector(".content-box").style.left = -(itemWidth * (index)) + 'px';
                },
                /**
                 *  触摸开始事件
                 */
                touchStart(e){
                    this.startX = e.touches[0].clientX;
                },
                /**
                 *  触摸移动事件
                 */
                touchmove(e){
                    this.moveX = e.touches[0].clientX;
                },
                /**
                 *  触摸结束事件
                 */
                touchend(e){
                    e.preventDefault();
                    if(this.moveX-this.startX > 0){
                        this.index = this.index-1;
                        this.cur = this.index;
                        if(this.index >= 0){
                            this.setMove(this.index);
                        }else{
                            this.index = 0;
                            this.cur = 0;
                        }
                    }else{
                        this.index = this.index +1;
                        if(this.index >= this.dataList.length-1){
                          this.index = this.dataList.length-1;
                        }
                        this.setMove(this.index);
                        this.cur = this.index;
                    }
                }
            },
		
		});
		
    </script>

项目地址:https://gitee.com/taojunhao/tabChange
如有需要可自行下载。
希望对大家有帮助。

[removed] $(function() { function setCurrentSlide(ele, index) { $(".swiper1 .swiper-slide").removeClass("selected"); ele.addClass("selected"); //swiper1.initialSlide=index; } var swiper1 = new Swiper('.swiper1', { // 设置slider容器能够同时显示的slides数量(carousel模式)。 // 可以设置为number或者 'auto'则自动根据slides的宽度来设定数量。 // loop模式下如果设置为'auto'还需要设置另外一个参数loopedSlides。 slidesPerView: 5.5, paginationClickable: true,//此参数设置为true时,点击分页器的指示点分页器会控制Swiper切换。 spaceBetween: 10,//slide之间的距离(单位px)。 freeMode: true,//默认为false,普通模式:slide滑动时只滑动一格,并自动贴合wrapper,设置为true则变为free模式,slide会根据惯性滑动且不会贴合。 loop: false,//是否可循环 onTab: function(swiper) { var n = swiper1.clickedIndex; } }); swiper1.slides.each(function(index, val) { var ele = $(this); ele.on("click", function() { setCurrentSlide(ele, index); swiper2.slideTo(index, 500, false); //mySwiper.initialSlide=index; }); }); var swiper2 = new Swiper('.swiper2', { //freeModeSticky 设置为true 滑动会自动贴合 direction: 'horizontal',//Slides的滑动方向,可设置水平(horizontal)或垂直(vertical)。 loop: false, // effect : 'fade',//淡入 //effect : 'cube',//方块 //effect : 'coverflow',//3D流 // effect : 'flip',//3D翻转 autoHeight: true,//自动高度。设置为true时,wrapper和container会随着当前slide的高度而发生变化。 onSlideChangeEnd: function(swiper) { //回调函数,swiper从一个slide过渡到另一个slide结束时执行。 var n = swiper.activeIndex; setCurrentSlide($(".swiper1 .swiper-slide").eq(n), n); swiper1.slideTo(n, 500, false); } }); }); [removed]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值