* {
margin: 0;
padding: 0;
}
#topNav {
width: 100%;
overflow: hidden;
font: 18px;
}
#topNav .swiper-slide {
padding: 0 10px;
width: auto;
text-align: center;
}
#topNav .swiper-slide span {
transition: all .3s ease;
display: block;
}
#topNav .active span {
color: #FF2D2D;
border-bottom: 3px solid #FF2D2D;
}
.tab-list {
display: none;
}
var mySwiper = new Swiper('#topNav', {
freeMode: true,
freeModeMomentumRatio: 0.5,
slidesPerView: 'auto',
});
swiperWidth = mySwiper.container[0].clientWidth
maxTranslate = mySwiper.maxTranslate();
maxWidth = -maxTranslate + swiperWidth / 2
$(".swiper-container").on('touchstart', function (e) {
e.preventDefault()
})
mySwiper.on('tap', function (swiper, e) {
// e.preventDefault()
slide = swiper.slides[swiper.clickedIndex]
slideLeft = slide.offsetLeft
slideWidth = slide.clientWidth
slideCenter = slideLeft + slideWidth / 2
// 被点击slide的中心点
mySwiper.setWrapperTransition(300)
if (slideCenter < swiperWidth / 2) {
mySwiper.setWrapperTranslate(0)
} else if (slideCenter > maxWidth) {
mySwiper.setWrapperTranslate(maxTranslate)
} else {
nowTlanslate = slideCenter - swiperWidth / 2
mySwiper.setWrapperTranslate(-nowTlanslate)
}
$("#topNav .active").removeClass('active');
$("#topNav .swiper-slide").eq(swiper.clickedIndex).addClass('active');
$(".tab-list").eq(swiper.clickedIndex).fadeIn().siblings('.tab-list').hide();
});
推荐一下我自己的前端学习群562862926,里面有大神总结的一套前端教学视频,欢迎有兴趣的朋友进群一起学习