首页 > 其他分享 >无缝轮播图

无缝轮播图

时间:2024-10-19 18:48:02浏览次数:1  
标签:const 轮播 items value props doms carousleList 无缝

```html

<template>   <!-- @touchend="handleTouchEnd" -->   <!-- @touchstart="handleTouchStart" -->   <div class="carousel-conainer">     <div       class="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">       <span         v-for="(childItem,childIndex) in items"         :key="childItem.id"         :class="{ active: currentIndex === childIndex }"         @click="currentIndex = childIndex"       ></span>     </div>   </div> </template> ``` ```js <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.offsetHeight     moveTo(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.offsetHeight     moveTo(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> ```   ```css
<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>

```

标签:const,轮播,items,value,props,doms,carousleList,无缝
From: https://www.cnblogs.com/xianlling/p/18478112

相关文章

  • 计算机视觉——人像的分割与无缝融合
    1.概述新加坡现代汽车集团创新中心的一篇新论文提供了一种在计算机视觉中分离“融合”人像的方法——在这些情况下,对象识别框架发现一个人在某种程度上与另一个人“太接近”(例如例如“拥抱”动作或“站在后面”的姿势),并且无法区分所代表的两个人,将他们与一个人或一个实体......
  • 网站后台修改首页轮播图?
    登录后台管理系统打开浏览器,输入后台管理系统的地址。输入用户名和密码进行登录。进入轮播图管理页面登录后,找到并点击“内容管理”或类似名称的菜单。在子菜单中选择“轮播图管理”或“首页轮播图”。编辑现有轮播图在轮播图列表中,找到需要修改的轮播图。点击......
  • 手风琴、轮播图案例
    摸鱼时,水一篇博客咯~~~分享两个小案例!!!!!!!!!!!!前言手风琴:仿王者荣耀做的一个小案例,结合JQuery.js轮播图:仿华泰保险官网首页图做的小案例,结合ant-design-vue轮播图使用一、手风琴效果图代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname=......
  • 网站后台修改模板?公司网站轮播如何修改?
    网站后台修改模板登录后台:使用管理员账号登录网站后台。导航至模板管理:在后台主界面中找到“模板管理”、“主题设置”或类似的选项。选择模板:从模板列表中选择当前使用的模板或想要切换的新模板。编辑模板:进入模板编辑页面,可以对模板的样式、布局等进行调整。......
  • vue表格轮播插件
    1.前言需求:制作大屏看板时,经常要展示表格数据,通常一页时放不下的,表格需要自动滚动,并维持表头固定为何自己封装:网上的滚动组件由2类,一种传入json数据进行滚动,优点是可以做到表头固定,但是样式不方便自定义(DataV),一直是常规滚动插件,不支持表头固定2.实现思路及说明使用插槽接......
  • 3D Carousel(3D轮播)
    3DCarouselJavaScript逻辑步骤归纳3D轮播图展示,动态添加图片,设置旋转动画,通过JavaScript和CSS3实现图片轮播效果。初始化图片列表定义一个数组imgList,其中包含所有图片的URL。letimgList=[];计算每张图片的旋转角度通过letdeg=360/imgList.length;......
  • 无缝数据流动:跨域数据交换的高效策略!
    大型企业为了业务拓展需要,会在全国乃至全球各地设立分公司和办事机构,以便更好地处理当地事务,并进行市场的开拓和客户维护,因此大型企业都会面临跨域数据交换的场景。跨域数据交换时,需要考虑多方面的问题,以确保传输的安全性、效率、合规性和成本效益:1、安全传输数据加密:在传输过......
  • ArgoWorkflow教程(六)---无缝实现步骤间参数传递
    之前我们分析了,Workflow、WorkflowTemplate、template3者之间如何传递参数。本文主要分析同一个Workflow中的不同step之间实现参数传递,比如将上一个步骤的输出作为下一个步骤的结果进行使用(而非以文件方式传递)。1.概述然后就是之前只分析了Workflow、WorkflowTemplat......
  • PbootCMS网站模板调用幻灯片轮播图及参数说明
    根据你提供的信息,我们可以详细解释如何使用 {pboot:slide} 标签及其相关的列表标签,并给出具体的示例和应用场景。适用范围全站任意地方均可使用 {pboot:slide} 标签来展示幻灯片。标签参数gid=*: 分组,必填,用于控制需要输出的幻灯片分组。num=*: 数量,非必填,用于控制需要......
  • PbootCMS模板调用幻灯片轮播图及参数说明
    我们可以详细解释如何使用 {pboot:slide} 标签以及相关的列表标签,并给出具体的示例和应用场景。适用范围全站任意地方均可使用 {pboot:slide} 标签来展示幻灯片。标签参数gid=*:分组,必填,用于控制需要输出的幻灯片分组。num=*:数量,非必填,用于控制需要输出的数量,默认为5个......