swiper 移动端选项卡_基于swiper的Tab选项卡

本文介绍了如何使用Swiper库创建移动端选项卡。通过HTML布局、CSS样式设计和JavaScript封装函数`tabs`,实现了选项卡的切换效果。文章详细讲解了每个部分的代码实现,并提供了Js调用示例,帮助读者理解并应用到自己的项目中。
摘要由CSDN通过智能技术生成

选项卡五花八门,今天又要用到选项卡,首选swiper!

一、HTML布局

根据swiper官网的要求来class命名滑块。

  • Div+CSS
  • JavaScript+JQuery
  • AngularJS+Vue+NodeJs
千寻Div+CSS
阿飞JavaScript+JQuery
无虑AngularJS+Vue+NodeJs

二、CSS样式

随便写写,根据使用场景调整。(PS:推荐一个在线美化工具)

*{margin:0;padding:0}li{list-style:none}.box{margin:50px auto;width:800px}.swiperTab{display:flex;width:100%;flex-direction:row;justify-content:center;align-items:center}.swiperTab li{display:flex;height:48px;border-left:1px solid #dfdfdf;background-color:#ddf8ff;cursor:pointer;flex:1;flex-direction:row;justify-content:center;align-items:center}.swiperTab li:first-child{border-left:1px solid transparent}.swiperTab li.active{background-color:#f60;color:#fff}.swiperTab li:nth-child(1).active{background-color:#9acd32}.swiperTab li:nth-child(2).active{background-color:green}.swiperTab li:nth-child(3).active{background-color:pink}.swiper-slide{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:20px}.swiper-slide:nth-child(1){height:200px;background-color:#9acd32}.swiper-slide:nth-child(2){height:300px;background-color:green}.swiper-slide:nth-child(3){height:100px;background-color:pink}

三、Js封装

自己封装的选项卡函数swiperTab.js

/* swiper 选项卡函数 参数说明* obj 必选,导航列表* swiperObj: 必选,HTML元素或者string类型,Swiper容器的css选择器* className: 必选,当前样式的类名* effect:可选,切换效果,默认为'slide',可设置为'fade,cube,coverflow,flip'。* 其他参数参阅官网 http://www.swiper.com.cn* */function tabs(obj,swiperObj,className) { var tabSwiper = new Swiper(swiperObj, { effect : 'flip',//切换效果 speed : 500, //滑动速度,单位ms autoHeight: true, // 高度随内容变化 onSlideChangeStart : function() { $(obj+'.'+className).removeClass(className); /*有当前类名的删除类名,给下一个添加当前类名*/ $(obj).eq(tabSwiper.activeIndex).addClass(className);/*activeIndex是过渡后的slide索引*/ } }); // 模拟点击事件,如果是移入事件,将mousedown 改为 mouseenter $(obj).on('touchstart mousedown', function(e) { e.preventDefault();/*清除默认事件*/ $(obj+'.'+className).removeClass(className); $(this).addClass(className); /*点击当前导航 改变当前样式*/ tabSwiper.slideTo($(this).index());/*滑动到对应的滑块*/ }); $(obj).click(function(e) { e.preventDefault();/*清除默认点击事件*/ });}

四、Js调用

首先引入相关js

if(hash){ value = hash.match(/\d/g).join(''); index = Number(value);/*字符串转换为数字*/ index = parseInt(index)%lens; } $tabList.eq(index).addClass('active'); tabs('.swiperTab > li','.swiper-container','active',index); });```

[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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值