当前位置:首页 > VUE

vue实现左右滑动代码

2026-01-21 20:39:05VUE

vue实现左右滑动代码

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

vue实现左右滑动代码

基础实现(使用 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 Router实现页面跳转 安装Vue Router后,在项目中配置…

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…