无缝轮播图

news/2024/10/19 19:03:49

## vue3 无缝轮播图

<template><!-- @touchend="handleTouchEnd" --><!-- @touchstart="handleTouchStart" --><div class="carousel-conainer"><divclass="carousel-list":style="{ transform: `translateX(-${currentIndex * 100}%)` }"><div v-for="(item) in items" :key="item.id" class="carousel-item"><img :src="item.image" :alt="item.title" /></div></div><div class="carousel-control prev" @click="prevSlide">&lt;</div><div class="carousel-control next" @click="nextSlide">&gt;</div><div class="indicator"><spanv-for="(childItem,childIndex) in items":key="childItem.id":class="{ active: currentIndex === childIndex }"@click="currentIndex = childIndex"></span></div></div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'const props = defineProps({items: {type: Array,required: true,validator: items => Array.isArray(items) && items.length > 0, // 增加验证,确保items为数组且不为空},autoplayInterval: {type: Number,default: 3000,},
})const currentIndex = ref(0)
// const touchStartX = ref(0)
const isVisible = ref(true)
// let autoplayInterval = null// const updateAutoplay = () => {
//   if (isVisible.value && props.autoplayInterval) {
//     autoplayInterval = setInterval(nextSlide, props.autoplayInterval)
//   } else {
//     clearInterval(autoplayInterval)
//   }
// }
// const doms = ref({
//   carousleList: document.querySelector('.carousel-list'),
//   indicator: document.querySelector('.indicator'),
//   prevBtn: document.querySelector('.prev'),
//   nextBtn: document.querySelector('.next'),
// })
const doms = ref({carousleList: null,indicator: null,
})
const count = ref(props.items.length)
const init = () => {doms.value.carousleList = document.querySelector('.carousel-list')doms.value.indicator = document.querySelector('.indicator')// 复制第一张图片到最后,复制最后一张图片到第一if (doms.value.carousleList) {const firstCloned = doms.value.carousleList.firstElementChild.cloneNode(true)const lastCloned = doms.value.carousleList.lastElementChild.cloneNode(true)doms.value.carousleList.appendChild(firstCloned)doms.value.carousleList.insertBefore(lastCloned,doms.value.carousleList.firstElementChild,)lastCloned.style.marginLeft = '-100%' } else {console.error("Carousel list not found");}// 绑定事件
}const moveTo = index => {doms.value.carousleList.style.transform = `translateX(-${index * 100}%)`doms.value.carousleList.style.transition = 'transform 0.5s ease'doms.value.indicator.querySelectorAll('span')[index].classList.add('active')doms.value.indicator.querySelectorAll('span')[currentIndex.value].classList.remove('active')currentIndex.value = index
}
const nextSlide = () => {if (currentIndex.value === count.value - 1) {doms.value.carousleList.style.transform = `translateX(100%)`doms.value.carousleList.style.transition = 'none'// 让浏览器渲染doms.value.carousleList.offsetHeightmoveTo(0)} else {moveTo((currentIndex.value + 1) % props.items.length)}
}
const prevSlide = () => {// if (props.items.length === 0) return // 防止没有项目时出错if (currentIndex.value === 0) {console.log('currentIndex.value === 0')doms.value.carousleList.style.transform = `translateX(-${count.value * 100}%)`doms.value.carousleList.style.transition = 'none'// 让浏览器渲染doms.value.carousleList.offsetHeightmoveTo(count.value - 1)}else{moveTo((currentIndex.value - 1 + props.items.length) % props.items.length)}
}// const nextSlide = () => {
//   if (props.items.length === 0) return // 防止没有项目时出错
//   currentIndex.value = (currentIndex.value + 1) % props.items.length
// }// const prevSlide = () => {
//   if (props.items.length === 0) return // 防止没有项目时出错
//   currentIndex.value =
//     (currentIndex.value - 1 + props.items.length) % props.items.length
// }const handleVisibilityChange = () => {isVisible.value = !document.hidden// updateAutoplay()
}// const handleTouchStart = e => {
//   if (e.touches.length > 0) {
//     touchStartX.value = e.touches[0].clientX
//   }
// }// const handleTouchEnd = e => {
//   if (e.changedTouches.length > 0) {
//     const touchEndX = e.changedTouches[0].clientX
//     const diff = touchStartX.value - touchEndX
//     if (Math.abs(diff) > 50) {
//       diff > 0 ? nextSlide() : prevSlide()
//     }
//   }
// }onMounted(() => {init()// updateAutoplay()document.addEventListener('visibilitychange', handleVisibilityChange)
})onUnmounted(() => {// clearInterval(autoplayInterval)document.removeEventListener('visibilitychange', handleVisibilityChange)
})// watch(isVisible, updateAutoplay)
</script><style scoped lang="scss">
.carousel-conainer {position: relative;width: 500px;// height: 400px;margin: 50px auto;outline: 1px solid #000;overflow: hidden;.carousel-list {display: flex;transition: transform 0.5s ease;height: 100%;.carousel-item {flex: 0 0 100%;height: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;}.carousel-item img {max-width: 100%;max-height: 80%;object-fit: contain;}}.carousel-control {position: absolute;top: 50%;width: 40px;height: 40px;transform: translateY(-50%);background: rgba(0, 0, 0, 0.4);border-radius: 50%;color: white;border: none;padding: 10px;cursor: pointer;display: flex;justify-content: center;align-items: center;&:hover {background: rgba(0, 0, 0, 1);}}.carousel-control.prev {left: 10px;}.carousel-control.next {right: 10px;}.indicator {position: absolute;bottom: 10px;left: 50%;transform: translateX(-50%);display: flex;justify-content: center;align-items: center;width: 100%;height: 20px;// background: rgba(0, 0, 0, 0.5);color: white;font-size: 12px;text-align: center;span {margin: 0 5px;width: 10px;height: 10px;border-radius: 50%;background: #fff;border: 1px solid #000;cursor: pointer;&.active {background: red;}}}
}
</style>
 
 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/73540.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

如何制作DIY一个低成本高性能的遥控玩具坦克?

前言 小学一年级的时候,第一次在深圳沙井的商场看到了遥控挖掘机,每次去那个商场我都会默默地去看一看挖土机玩具,那时就萌生了一个梦想,长大了一定要买一个遥控挖掘机。时光荏苒,儿时的梦想,如今依然记得,现在有能力买挖掘机,但是却没有了买一台挖掘机玩具的想法了。随…

【GIC】GICv3 基本规则

本章介绍了符合GICv3架构的中断控制器的基本操作。它还描述了不同的编程接口。 一.中断类型SPI(Shared Peripheral Interrupt)--共享外设中断​ 这是一个全局外设中断,可以路由到指定的PE,或路由到一组PE中的一个。PPI (Private Peripheral Interrupt)--私有外设中断​ …

直线与圆的最值问题(高二)

直线与圆的最值问题专题:直线+圆 \(\qquad \qquad\) 题型:最值问题 \(\qquad \qquad\) 难度系数:★★★题目 已知\(P\)为圆\(C:x^2+y^2=1\)上的动点,直线\(l_1:kx-y-3k=0\)恒过定点\(A\),\(Q\)为直线\(l_2:x-y+3=0\)上的动点,则\(|PA|+3|PQ|\)的最…

网站后台模板前台修改?网站后台的界面如何修改?

需求分析:明确需要修改的具体内容,包括文本、图片、颜色、布局等。 功能开发:如果是简单的文本或图片修改,确保后台有相应的编辑功能。 对于样式调整,可以增加一个CSS编辑器或者提供样式选择器。 布局调整则需要更复杂的拖拽组件支持。测试:在不同设备和浏览器上测试修改…

网站模板的logo框架修改?后台修改网站内容?

确定Logo位置打开网站模板的HTML文件,找到放置Logo的HTML元素。通常这个元素会有一个特定的类名或ID,如<div id="logo">或<div class="logo">。调整CSS样式在CSS文件中找到与Logo相关的样式规则。这些规则可能包括宽度、高度、背景图像、边距…

网站模板怎么修改字体?公司网站地图怎么修改?

修改网站模板中的字体通过CSS修改打开网站模板的CSS文件。 查找与字体相关的样式规则,如 font-family, font-size 等。 修改这些属性以应用新的字体设置。例如:body {font-family: Arial, sans-serif;font-size: 16px; }使用内联样式如果模板不支持外部CSS文件,可以在HTML标…

网站后台在线客服修改?网站模板如何修改?

登录后台管理系统使用管理员账号登录到网站的后台管理系统。导航至客服设置在后台管理界面中,找到并点击“客服管理”或“在线客服设置”等相关选项。编辑客服信息在客服设置页面,可以编辑客服的基本信息,如客服名称、联系方式、工作时间等。 如果支持多客服,可以选择或添加…

修改帝国网站登录密码?网站被人修改了密码?

修改帝国网站(如帝国CMS)的登录密码可以通过以下几种方式实现:通过后台管理界面修改:登录到帝国CMS的后台管理界面。 进入“系统”->“系统设置”->“管理员密码修改”。 按照提示输入新密码并保存。通过数据库直接修改:使用数据库管理工具(如phpMyAdmin)登录到你…