首页 > 其他分享 >vue无缝循环轮播

vue无缝循环轮播

时间:2023-08-18 11:25:14浏览次数:56  
标签:vue translateX 轮播 value item translateValue const 无缝

在网上看了几个无缝循环轮播的实现方法,使用方法都比较复杂,所以这里提供一种比较简单的实现方式

gitee: https://gitee.com/philippines-kisses-snow/rotation

结构和理论梳理

理论

image
轮播的原理就是通过translateY/translateX移动轮播容器+过渡达到轮播项上下左右轮播效果的,为了达到无缝循环效果,这里在轮播容器的末尾追加第一项内容,如图:【“1”,“2”,“3”,“4”】这四项是我想轮播的内容,此时在容器末尾还追加了“1”,当轮播到“4”时,“4”与“1”之间的切换就达到了无缝效果,在切换到末尾的“1”时,等待过渡结束后取消过渡,立即切换到开头的“1”,这样就达到了将末尾切换到开始的循环

结构

  <div>
    <div>
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>1</div>
    </div>
  </div>

上下轮播代码

image

<template>
  <div style="height: 200px;overflow: hidden;">
    <div class="box" :style="{ transitionDuration: `${transitionDuration}ms`, transform: `translateY( ${translateX}px)`}">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>1</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
// 当前滚动高度
const translateX = ref(0)
// 过渡时间
const transitionDuration = ref(500)

// 每次滚动高度
const translateValue = 200
// 滚动项个数
const switeCount = 4

onMounted(() => {
  setInterval(() => {
    translateX.value -= translateValue
    transitionDuration.value = 500
    if(translateX.value === -translateValue * switeCount) {
      setTimeout(() => {
        translateX.value -= translateValue
        transitionDuration.value = 0
        translateX.value = 0
      }, 600)
    }
  }, 2000)
})

</script>

<style scoped>

.box div {
  width: 300px;
  height: 200px;
  background-color: aquamarine;
}
</style>

基于这个理论的轮播表格

image

<template>
  <div style="width: 800px;" class="table">
    <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0">
      <thead style="background-color: #333F4F;">
        <tr :style="{ height: rowHeight + 'px'}">
          <th style="max-width: 60px;width: 60px;">NO.</th>
          <th style="max-width: 120px;width: 120px;">客户代码</th>
          <th style="max-width: 120px;width: 120px;">客户名称</th>
          <th style="max-width: 120px;width: 120px;">提货人</th>
          <th style="max-width: 120px;width: 120px;">车牌</th>
          <th style="max-width: 120px;width: 120px;">出货日</th>
          <th>目的地</th>
        </tr>
      </thead>
    </table>
    <div style="overflow: hidden;position: relative;top: -2px;" :style="{ height: (Math.min(maxRow, tableData.length) * rowHeight) + 'px' }">
      <table  border="0" cellpadding="0" cellspacing="0" style="width: 100%;" :style="{ transitionDuration: `${transitionDuration}ms`, transform: `translateY( ${translateX}px)`}">
        <tbody style="background-color: #333F4F;">
          <tr v-for="(item, index) in tableData" :key="index" :style="{ height: rowHeight + 'px'}">
            <td style="max-width: 60px;width: 60px;text-align: center;">{{ item.demo1 }}</td>
            <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo2 }}</td>
            <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo3 }}</td>
            <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo4 }}</td>
            <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo5 }}</td>
            <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo6 }}</td>
            <td style="text-align: center;">{{ item.demo7 }}</td>
          </tr>
          <template v-for="(item, index) in tableData" :key="index">
            <tr v-if="index < maxRow && tableData.length > maxRow" :style="{ height: rowHeight + 'px' }">
              <td style="max-width: 60px;width: 60px;text-align: center;">{{ item.demo1 }}</td>
              <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo2 }}</td>
              <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo3 }}</td>
              <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo4 }}</td>
              <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo5 }}</td>
              <td style="text-align: center;max-width: 120px;width: 120px;">{{ item.demo6 }}</td>
              <td style="text-align: center;">{{ item.demo7 }}</td>
            </tr>
          </template>
        </tbody>
      </table>
    </div>
  </div>  
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { tableData } from './table';

const translateX = ref(0)
const transitionDuration = ref(500)

const maxRow = 8
const rowHeight = 48
const translateValue = rowHeight
const switeCount = tableData.length

onMounted(() => {
  setInterval(() => {
    if(tableData.length > maxRow) {
      translateX.value -= translateValue
      transitionDuration.value = 500
      if(translateX.value === -translateValue * switeCount) {
        setTimeout(() => {
          translateX.value -= translateValue
          transitionDuration.value = 0
          translateX.value = 0
        }, 600)
      }
    }
  }, 2000)
})

</script>

<style scoped>

.box div {
  width: 300px;
  height: 200px;
  background-color: aquamarine;
}

table {
  color: #fff;
}

.table {
  border: 1px solid #046DB8;
  margin: auto;
}

table {
  color: #fff;
}
td {
  border-top: 1px solid #046DB8;
  border-bottom: 1px solid #046DB8;
  border-left: 1px solid #046DB8;
}

th {
  border-left: 1px solid #046DB8;
}

tr th:nth-child(1) {
  border-left: 0 !important;
}


tr td:nth-child(1) {
  border-left: 0 !important;
}
</style>

标签:vue,translateX,轮播,value,item,translateValue,const,无缝
From: https://www.cnblogs.com/my-wl/p/17639913.html

相关文章

  • 谈谈Vue3中的ref和reactive
    一、是什么?ref和reactive是Vue3中用来实现数据响应式的API一般情况下,ref定义基本数据类型,reactive定义引用数据类型(我喜欢用它来定义对象,不用它定义数组,原因后面讲)我理解的ref本质上是reactive的再封装二、先聊reactivereactive定义引用数据类型(以对象和数组举例),它能够将复......
  • ElementUI——vue2+element-ui 2.x的动态表格和表单
    前言一个基于vue2.x+element-ui2.x版本的项目,里面都是CURD的东西,但是前人并未封装组件,而是直接CV,现在要新增一个大模块的功能,就想着封装个组件,后面再基于这个组件对老项目进行改造;虽然是一个大模块,但是功能还是比较简单的,结构如下;内容?>这纯粹是个简单的DEMO,如果你需要......
  • vue-treeselect 树下拉组件被遮挡问题
    vue-treeselect组件官方中文网站: https://www.vue-treeselect.cn/需求背景:在el-tabs内容中添加此组件出现被遮挡问题通过文档查询解决方法<treeselectv-model="params.wardIds":options="hospitalWardTree"value-consists-of="LEAF_PRIORITY"placeholder=......
  • vue与js
    1.js中(...)用法  https://blog.csdn.net/snackpdd/article/details/119388250                     ---......
  • vue3项目,vie框架,相对路径图片,测试时正常显示,发布后不显示问题解决方案
    参考Vite官网的说明,修改图片的引用路径后,图片发布后可以正常显示constimgUrl=newURL('./img.png',import.meta.url).hrefdocument.getElementById('hero-img').src=imgUrl官网地址: https://cn.vitejs.dev/guide/assets.html ......
  • Vue学习笔记:Vuex Part04 Getter
    Getter可以将store的state派生出一些状态,比如根据条件进行过滤Getter接受state作为其第一个参数,示例:conststore=createStore({state:{todos:[{id:1,text:'...',done:true},{id:2,text:'...',done:false}]},getters:{......
  • 安装VS Code并配置Vue开发环境
    VSCode是一款轻量级、功能强大的代码编辑器,支持多种编程语言和平台。它不仅提供了基本的文本编辑功能,还集成了终端、调试器、版本控制等工具,使得开发工作更加高效。以下是安装VSCode的步骤:在浏览器中打开VSCode官方网站(https://code.visualstudio.com/),点击下载适合自己操作系统......
  • MTvue
    标签a-space标签:这是一个AntDesign的a-space组件,用于在内部放置一些元素,并根据需要添加间距。AntDesign:是一个基于React的企业级UI组件库,提供了丰富的可复用的UI组件和样式。a-button标签<a-buttontype="primary"@click="handleAdd">新增</a-button>这是一个Ant......
  • Vue的数据更新,页面不更新的解决办法
    可能原因更新的数据跟源数据不是同一个,即不是同一个引用解决办法最稳妥的办法,可通过拿到源数据取索引的方式进行数据的更新,如:有一个源数据叫:originData那么如果在更新时,通过this.originData[index].time=newValue的方式进行更新,而不是item.time=newValue这样,更新的是源......
  • vue2使用vite
    安装插件//一个vite必备,第二个是为了兼容vue2npmi-Dvitevite-plugin-vue2将public中的index.html拉出来,放在最外层,与package.json同级在index.html中引入main.js<!DOCTYPEhtml><htmllang="en"><head><metacharset="utf-8"><metahttp-e......