当前位置:首页 > VUE

vue实现左右滑动代码

2026-01-21 20:39:05VUE

vue实现左右滑动代码

以下是一个基于 Vue 3 实现左右滑动功能的代码示例,结合手势识别和动画效果:

基础实现(使用 CSS 和事件监听)

<template>
  <div class="slider-container" @touchstart="startDrag" @touchmove="onDrag" @touchend="endDrag">
    <div class="slider" :style="{ transform: `translateX(${offsetX}px)` }">
      <div v-for="item in items" :key="item.id" class="slide">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const items = [
  { id: 1, content: 'Slide 1' },
  { id: 2, content: 'Slide 2' },
  { id: 3, content: 'Slide 3' }
];

const offsetX = ref(0);
let startX = 0;
let isDragging = false;

const startDrag = (e) => {
  startX = e.touches[0].clientX;
  isDragging = true;
};

const onDrag = (e) => {
  if (!isDragging) return;
  const currentX = e.touches[0].clientX;
  offsetX.value = currentX - startX;
};

const endDrag = () => {
  isDragging = false;
  // 添加滑动结束后的逻辑(如自动对齐到最近幻灯片)
  offsetX.value = 0; // 示例:简单复位
};
</script>

<style>
.slider-container {
  width: 100%;
  overflow: hidden;
  position: relative;
}

.slider {
  display: flex;
  transition: transform 0.3s ease;
}

.slide {
  flex: 0 0 100%;
  min-height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #eee;
  border: 1px solid #ddd;
}
</style>

增强版(使用第三方库)

安装 Hammer.js 实现更专业的手势识别:

npm install hammerjs
<template>
  <div ref="slider" class="slider-container">
    <div class="slider" :style="{ transform: `translateX(${currentIndex * -100}%)` }">
      <div v-for="item in items" :key="item.id" class="slide">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import Hammer from 'hammerjs';

const items = [
  { id: 1, content: 'Slide 1' },
  { id: 2, content: 'Slide 2' },
  { id: 3, content: 'Slide 3' }
];

const currentIndex = ref(0);
const slider = ref(null);

onMounted(() => {
  const mc = new Hammer(slider.value);
  mc.on('swipeleft', () => {
    if (currentIndex.value < items.length - 1) {
      currentIndex.value++;
    }
  });
  mc.on('swiperight', () => {
    if (currentIndex.value > 0) {
      currentIndex.value--;
    }
  });
});
</script>

<style>
.slider-container {
  width: 100%;
  overflow: hidden;
}

.slider {
  display: flex;
  transition: transform 0.5s ease-out;
}

.slide {
  flex: 0 0 100%;
  min-height: 300px;
}
</style>

带指示器的完整组件

<template>
  <div class="carousel">
    <div class="slides" :style="{ transform: `translateX(${-currentIndex * 100}%)` }">
      <div v-for="(slide, index) in slides" :key="index" class="slide">
        <img :src="slide.image" alt="">
      </div>
    </div>
    <div class="indicators">
      <span 
        v-for="(_, index) in slides" 
        :key="index" 
        :class="{ active: currentIndex === index }"
        @click="goToSlide(index)"
      ></span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const slides = [
  { image: 'https://via.placeholder.com/800x400?text=Slide+1' },
  { image: 'https://via.placeholder.com/800x400?text=Slide+2' },
  { image: 'https://via.placeholder.com/800x400?text=Slide+3' }
];

const currentIndex = ref(0);

const goToSlide = (index) => {
  currentIndex.value = index;
};

// 自动轮播
onMounted(() => {
  setInterval(() => {
    currentIndex.value = (currentIndex.value + 1) % slides.length;
  }, 3000);
});
</script>

<style>
.carousel {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.slides {
  display: flex;
  transition: transform 0.5s ease;
}

.slide {
  flex: 0 0 100%;
}

.slide img {
  width: 100%;
  display: block;
}

.indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
}

.indicators span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
}

.indicators span.active {
  background: white;
}
</style>

关键实现要点

  • 使用 CSS 的 transform: translateX 实现横向移动
  • 通过 flexbox 布局确保幻灯片横向排列
  • 触摸事件处理需考虑 touchstarttouchmovetouchend
  • 添加 transition 属性实现平滑动画效果
  • 使用第三方手势库(如 Hammer.js)可增强交互体验
  • 通过计算当前索引值控制幻灯片位置

可根据实际需求调整滑动阈值、动画时长和响应式断点等参数。

vue实现左右滑动代码

标签: 代码vue
分享给朋友:

相关文章

vue实现视频开场

vue实现视频开场

使用 Vue 实现视频开场动画 在 Vue 中实现视频开场动画可以通过结合 HTML5 的 <video> 标签和 Vue 的过渡效果或生命周期钩子来完成。以下是具体实现方法: 安装依赖…

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…

vue播放倍速怎么实现

vue播放倍速怎么实现

实现Vue播放倍速的方法 在Vue中实现播放倍速功能通常涉及HTML5的<video>元素或第三方播放器库。以下是几种常见实现方式: 使用原生HTML5 video元素 通过ref获取…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterv…