vue-awesome-swiper 实现 tab 切换效果

vue-awesome-swiper 实现 tab 切换效果

Github

vue-awesome-swiper 3.x

安装 vue-awesome-swiper 3.x 时会自动安装 swiper 4.x

npm install vue-awesome-swiper@3.1.3 --save
<template>
	<div id="app">
		<h5swiper
			ref="swiperTitle"
			class="swiper-title"
			:options="swiperOptionTitle"
			:auto-update="true"
			:auto-destroy="true"
			:delete-instance-on-destroy="true"
			:cleanup-styles-on-destroy="true"
		>
			<swiper-slide
				v-for="(item, index) of tabListData"
				ref="swiperSlideItem"
				:key="'name' + index"
				:iname="item.name"
				class="swiper-slide-title"
			>
				<div
					class="tab-name"
					:class="{ active: index === swiperActiveIndex }"
					@click="handleSlidClickFun(index)"
				>
					{{ item.name }}
				</div>
			</swiper-slide>
		</h5swiper>
		<h5swiper
			ref="swiperContent"
			class="swiper-content"
			:options="swiperOptionContent"
			:auto-update="true"
			:auto-destroy="true"
			:delete-instance-on-destroy="true"
			:cleanup-styles-on-destroy="true"
		>
			<swiper-slide
				v-for="(item, index) of tabListData"
				:key="'content' + index"
				class="swiper-slide-content"
			>
				{{ item.name }}
			</swiper-slide>
		</h5swiper>
	</div>
</template>

<script>
//  vue-awesome-swiper 官网 https://github.surmon.me/vue-awesome-swiper/
import { swiper as h5swiper, swiperSlide } from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
export default {
	components: {
		h5swiper,
		swiperSlide,
	},
	data() {
		const _this = this
		return {
			swiperActiveIndex: 0,
			titleActive: '',
			swiperOptionContent: {
				slidesPerView: 'auto',
				on: {
					slideChangeTransitionStart: function() {
						_this.swiperActiveIndex = this.activeIndex
						_this.swiperTitle.slideTo(this.activeIndex, 500, false)
					},
				},
			},
			swiperOptionTitle: {
				slidesPerView: 'auto',
				freeMode: true,
			},
			tabListData: [
				{
					name: '直播',
				},
				{
					name: '推荐',
				},
				{
					name: '追番',
				},
				{
					name: '热门',
				},
				{
					name: '影视',
				},
				{
					name: '奥运',
				},
				{
					name: '建党百年',
				},
			],
		}
	},
	computed: {
		swiperContent() {
			return this.$refs.swiperContent.$el.swiper
		},
		swiperTitle() {
			return this.$refs.swiperTitle.$el.swiper
		},
	},
	mounted() {},
	methods: {
		handleSlideToFun(index) {
			this.swiperActiveIndex = index
			this.swiperContent.slideTo(index, 500, false)
			this.swiperTitle.slideTo(index, 500, false)
		},
		handleSlidClickFun(index) {
			this.handleSlideToFun(index)
		},
	},
}
</script>

<style lang="scss">
.swiper-slide-title {
	width: auto !important;
	.tab-name {
		padding: 10px;
		&.active {
			color: #2ea9fe;
		}
	}
}
.swiper-slide-content {
	padding: 10px;
}
</style>

vue-awesome-swiper 4.x

使用 vue-awesome-swiper 4.x,需要同时安装 vue-awesome-swiper 和 swiper

npm install swiper vue-awesome-swiper --save

package.json

"dependencies": {
	"swiper": "^6.8.1",
	"vue-awesome-swiper": "^4.1.1",
},

使用方式和 vue-awesome-swiper 3.x 相同,引用方式差异:

import { Swiper, SwiperSlide } from "vue-awesome-swiper";
import "swiper/swiper-bundle.css";
export default {
  components: {
    Swiper,
    SwiperSlide,
  },
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值