我在网上查了一下,说再次启用,只需要再次调用即可,但是我调用了为啥没效果
<template>
<!-- 此页面为图片轮播页面 -->
<div id="kuang_jia" data-v-app>
<div id="img">
<div id="imgContainer" ref="divImage">
<!-- 标签中的ref的作用和js中的document.getElementById作用一样.都是获取标签属性 -->
<img v-for="(item, index) in imgList.array" :key="index" alt="" v-bind:src="item">
<!-- 设置图片数组循环,依次获取所有图片 -->
</div>
<div class="zuo_you">
<span class="zuo" @click="zuo">
<
</span>
<span class="you" @click="you">
>
</span>
</div>
<!-- 箭头div必须放在图片div的后面,否则不显示 -->
</div>
</div>
</template>
<script setup>
import { onMounted, reactive, ref, watch } from 'vue'
import one from '../../image/0a96e2a01593e9c8ce9ff190b1957172.jpg'
import tow from '../../image/0b2f6c5e2a7ea37e1f6cc2dd2d38f2f1.jpg'
import three from '../../image/1ac1c7bf66c43e0cd3ca51699c5206ba.jpeg'
import four from '../../image/5fec7cd428d3edd59870cf23c7c4149f.jpeg'
import five from '../../image/33dcfaedf645b04a7ded7e3a64b897ff.jpg'
import six from '../../image/9477156de6d94c49c505a9dfce8e8c17.jpg'
import seven from '../../image/c52be335098658931847fa0ecbcf7d03.jpeg'
import eight from '../../image/d86d80e8573ab91914a6e9ad5217c785.jpg'
import nine from '../../image/e305fca1a381bda8482d8b3296da61f6.jpg'
import ten from '../../image/fa983053c19cf3c7b27491507799e02e.jpeg'
// 导入所有图片
const imgList = reactive({ array: [] })
imgList.array = [one, tow, three, four, five, six, seven, eight, nine, ten]
// 将图片放入数组,以供循环
const divImage = ref()
// 获取标签属性
const countChu = ref()
// 设置滚动初始值
const width = ref()
const xunHuan1 = setInterval(() => {
width.value = (-10) * countChu.value
divImage.value.style.transform = 'translateX(' + width.value + '%)'
// 设置每次轮播的距离百分比
divImage.value.style.transitionDuration = '1s'
// 设置每次轮播过程的时间
countChu.value++
// 设置次数的增长
if (countChu.value >= 5) {
clearInterval(xunHuan1)
// 当次数大于等于5时,停止滚动
}
}, 3000)
// 设置滚动,由于水平原因无法实现轮播,故设置为当页面最右边的图片为最后一张时,停止滚动
const zuo = () => {
if (countChu.value <= 1) {
return
}
width.value += 10
countChu.value--
divImage.value.style.transform = 'translateX(' + width.value + '%)'
}
// 设置左侧箭头点击事件
const you = () => {
if (countChu.value >= 5) {
return
}
width.value -= 10
countChu.value++
divImage.value.style.transform = 'translateX(' + width.value + '%)'
}
// 设置右侧箭头点击事件
watch(countChu.value, () => {
xunHuan1()
})
// 当次数小于5时,继续启用定时器
onMounted(() => {
countChu.value = 1
})
// 挂载执行
</script>
<style scoped>
#kuang_jia {
position: relative;
width: 100%;
margin: 0 auto;
}
img {
width: 200px;
height: 300px;
margin: 20px;
border: azure 3px solid;
}
#img {
width: 1230px;
height: 340px;
margin: 0 auto;
position: relative;
overflow: hidden;
}
#img div {
width: 2460px;
height: 340px;
position: relative;
background-color: bisque;
}
.zuo_you {
position: absolute !important;
width: 1200px !important;
background-color: transparent !important;
top: 0;
left: 0;
}
/* 设置顶层显示,并且上下居中 */
.zuo, .you {
font-size: 50px;
position: relative;
cursor: pointer;
display: inline-block;
width: 50px;
height: 50px;
color: aqua;
}
.zuo {
position: absolute !important;
top: 50%;
transform: translateY(-50%);
}
.you {
position: absolute !important;
top: 50%;
right: -3%;
transform: translateY(-50%);
}
/* 扩展:!important(Css属性) */
/* 用法: https://www.runoob.com/css/css-important.html */
</style>
当使用setInterval时,仅当没有该定时器的任何其他代码实例时,才将定时器添加到队列中。这确保了定时器代码添加到队列中的最小时间间隔为指定时间间隔。
有时候会出现以下问题:
举例: 某个onclick事件使用serInterval设置了一个200ms间隔的重复定时器,如果时间处理程序花了300多ms时间完成,同时定时器也花了差不多的时间,就会同时出现跳过间隔且连续运行定时器代码的情况。如图:
解决方法: 可以尝试使用 链式setTimeout()
//链式使用
setTimeout(function () {
setTimeout(arguments.callee, interval)
}, interval)
问题点:定时器重启无效
分析:
①有没有可能定时器调用的函数是因为图片到了最后一张时,停止滚动,需要回到起始。
解决办法:
出现情况①,判断图片是最后一张,则属性初始化,重头执行.