Vue3.0手寫輪播圖效果
本文實例為大家分享了Vue3.0手寫輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下
讓我們開始把
html結(jié)構(gòu)
<template>
<div class="xtx-carousel" @mouseleave="enterFn" @mouseenter="leaveFn">
<ul class="carousel-body">
<li class="carousel-item " :class="{ fade: indexid === index }" v-for="(item, index) in list" :key="item.id">
<RouterLink to="/">
<img :src="item.imgUrl" alt="" />
</RouterLink>
</li>
</ul>
<a href="javascript:;" class="carousel-btn prev" @click="lastPage"><i class="iconfont icon-angle-left"></i></a>
<a href="javascript:;" class="carousel-btn next" @click="nextPage"><i class="iconfont icon-angle-right"></i></a>
<div class="carousel-indicator">
<span @click="indexid = i - 1" v-for="i in list.length" :key="i" :class="{ active: indexid === i - 1 }"></span>
</div>
</div>
</template>
js語法
<script>
import { ref, watch, onUnmounted } from 'vue'
export default {
name: 'Carousel',
props: {
// 圖片數(shù)據(jù)
list: {
type: Object,
default: () => {}
},
// 輪播圖每張切換的事件
duration: {
type: Number,
default: 2
},
// 是否開啟輪播圖
autoplay: {
type: Boolean,
default: true
},
// 點擊翻幾張
page: {
type: Number,
default: 1
}
},
setup(props) {
// 索引
const indexid = ref(0)
// 輪播
const timer = ref(null)
const TimeFn = () => {
clearInterval(timer)
// 每次執(zhí)行前都清除上一次的定時器
timer.value = setInterval(() => {
indexid.value++
// 如果超出界限就重新回填
if (indexid.value > props.list.length - 1) {
indexid.value = 0
}
}, props.duration * 1000)
}
// 點擊+下一站圖片
const nextPage = () => {
indexid.value += props.page
if (indexid.value > props.list.length - 1) {
indexid.value = 0
}
}
// 點擊+上一張圖片
const lastPage = () => {
indexid.value -= props.page
if (indexid.value < 0) {
indexid.value = props.list.length - 1
}
}
// 清除定時器
const leaveFn = () => {
// console.log('清除定時器')
clearInterval(timer.value)
// console.log(timer)
}
// 組件消耗,清理定時器
onUnmounted(() => {
clearInterval(timer.value)
})
// 開啟定時器
const enterFn = () => {
if (props.list.length > 1 && props.autoplay) {
// console.log('開啟定時器')
TimeFn()
}
}
watch(
() => props.list,
() => {
if (props.list.length > 1 && props.autoplay) {
TimeFn()
}
}
)
return { indexid, leaveFn, enterFn, nextPage, lastPage }
}
}
</script>
css樣式
<style scoped lang="less">
.xtx-carousel {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.carousel {
&-body {
width: 100%;
height: 100%;
}
&-item {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
transition: opacity 0.5s linear;
&.fade {
opacity: 1;
z-index: 1;
}
img {
width: 100%;
height: 100%;
}
}
&-indicator {
position: absolute;
left: 0;
bottom: 20px;
z-index: 2;
width: 100%;
text-align: center;
span {
display: inline-block;
width: 12px;
height: 12px;
background: rgba(0, 0, 0, 0.2);
border-radius: 50%;
cursor: pointer;
~ span {
margin-left: 12px;
}
&.active {
background: #fff;
}
}
}
&-btn {
width: 44px;
height: 44px;
background: rgba(0, 0, 0, 0.2);
color: #fff;
border-radius: 50%;
position: absolute;
top: 228px;
z-index: 2;
text-align: center;
line-height: 44px;
opacity: 0;
transition: all 0.5s;
&.prev {
left: 20px;
}
&.next {
right: 20px;
}
}
}
&:hover {
.carousel-btn {
opacity: 1;
}
}
}
</style>
注冊成全局插件
import Carousel from '../carousel.vue'
// vue2.0插件寫法要素:導(dǎo)出一個對象,有install函數(shù),默認(rèn)傳入了Vue構(gòu)造函數(shù),Vue基礎(chǔ)之上擴(kuò)展
// vue3.0插件寫法要素:導(dǎo)出一個對象,有install函數(shù),默認(rèn)傳入了app應(yīng)用實例,app基礎(chǔ)之上擴(kuò)展
export default {
install(app) {
// 在app上進(jìn)行擴(kuò)展,app提供 component directive 函數(shù)
// 如果要掛載原型 app.config.globalProperties 方式
app.component(Carousel.name, xtxCarousel)
}
}
在main.js入口文件掛載
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import libraryUI from '@/components/library/index' //自己的插件
createApp(App)
.use(store)
.use(router)
.use(libraryUI) // 掛載插件
.mount('#app')
如何使用插件呢?

<Carousel :list="list" duration="1" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue動態(tài)配置模板 ''component is''代碼
這篇文章主要介紹了vue動態(tài)配置模板 'component is'代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
通過Vue實現(xiàn)Excel文件的上傳和預(yù)覽功能
在業(yè)務(wù)系統(tǒng)中,Excel 文件作為一種常用的數(shù)據(jù)存儲和傳輸格式,經(jīng)常需要被處理和展示,這篇文章將講解如何通過 Vue 和 xlsx.js 實現(xiàn) Excel 文件的上傳和預(yù)覽功能,需要的朋友可以參考下2025-04-04
使用Vue3實現(xiàn)交互式雷達(dá)圖的代碼實現(xiàn)
雷達(dá)圖是一種可視化數(shù)據(jù)的方式,用于比較多個類別中不同指標(biāo)的相對值,它適用于需要展示多個指標(biāo)之間的關(guān)系和差異的場景,本文給大家介紹了如何用Vue3輕松創(chuàng)建交互式雷達(dá)圖,需要的朋友可以參考下2024-06-06
vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法
這篇文章介紹了vue3.0報錯Cannot?find?module‘worker_threads‘的解決辦法。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Vue3中實現(xiàn)代碼高亮的兩種方法(prismjs和highlight.js)
最近忙著開發(fā)自己的博客系統(tǒng),在做界面展示的時候,需要讓代碼高亮,于是經(jīng)過在網(wǎng)上查閱,發(fā)現(xiàn)有兩款比較好用的插件實現(xiàn)代碼高亮,分別是prismjs和highlight.js,下面我分別介紹下,方便給需要的同學(xué)參考2025-04-04
vue動態(tài)路由實現(xiàn)多級嵌套面包屑的思路與方法
在實際項目中我們會碰到多層嵌套的組件組合而成,比如我們常見的面包屑導(dǎo)航,下面這篇文章就來給大家介紹關(guān)于vue實現(xiàn)動態(tài)路由多級嵌套面包屑的思路與方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08

