当前位置:首页 > 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 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <tem…

vue实现按钮刷新

vue实现按钮刷新

Vue 实现按钮刷新功能 在 Vue 中实现按钮刷新功能可以通过多种方式实现,具体取决于刷新需求是局部刷新还是全局刷新。以下是几种常见的方法: 使用 window.location.reload()…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…