小說APP源碼,手動滑動輪播圖時,輪播圖跟隨移動實現的相關代碼
html結構
<template>
<div @mouseleave="enterFn" @mouseenter="leaveFn">
<ul>
<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>
<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: {
// 圖片數據
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)
// 每次執行前都清除上一次的定時器
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>
.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>
以上就是小說APP源碼,手動滑動輪播圖時,輪播圖跟隨移動實現的相關代碼, 更多內容歡迎關注之后的文章