当前位置:首页 > VUE

vue实现页面左右滑动

2026-02-21 23:41:54VUE

Vue 实现页面左右滑动

在 Vue 中实现页面左右滑动可以通过多种方式完成,以下是几种常见的方法:

使用 CSS 和 Vue 事件

通过 CSS 的 transform 和 Vue 的事件监听实现滑动效果。

<template>
  <div class="slider-container" @touchstart="handleTouchStart" @touchmove="handleTouchMove">
    <div class="slider-content" :style="{ transform: `translateX(${translateX}px)` }">
      <div class="slide">Slide 1</div>
      <div class="slide">Slide 2</div>
      <div class="slide">Slide 3</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      translateX: 0,
      startX: 0,
    };
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      const currentX = e.touches[0].clientX;
      this.translateX = currentX - this.startX;
    },
  },
};
</script>

<style>
.slider-container {
  overflow: hidden;
  width: 100%;
}
.slider-content {
  display: flex;
  transition: transform 0.3s ease;
}
.slide {
  min-width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #eee;
  border: 1px solid #ccc;
}
</style>

使用第三方库(如 Swiper)

Swiper 是一个流行的滑动库,支持 Vue 集成。

安装 Swiper:

npm install swiper vue-awesome-swiper

使用示例:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/swiper-bundle.css';

export default {
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      swiperOptions: {
        slidesPerView: 1,
        spaceBetween: 30,
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
    };
  },
};
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
.swiper-slide {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #eee;
  border: 1px solid #ccc;
}
</style>

使用 Vue 动画过渡

通过 Vue 的 <transition-group> 实现滑动动画。

<template>
  <div class="slider">
    <button @click="prev">Prev</button>
    <transition-group name="slide" tag="div" class="slider-content">
      <div v-for="(slide, index) in slides" :key="index" v-show="currentIndex === index" class="slide">
        {{ slide }}
      </div>
    </transition-group>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      slides: ['Slide 1', 'Slide 2', 'Slide 3'],
      currentIndex: 0,
    };
  },
  methods: {
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length;
    },
  },
};
</script>

<style>
.slider {
  display: flex;
  align-items: center;
  justify-content: center;
}
.slider-content {
  position: relative;
  width: 300px;
  height: 200px;
  overflow: hidden;
}
.slide {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #eee;
  border: 1px solid #ccc;
}
.slide-enter-active, .slide-leave-active {
  transition: all 0.5s ease;
}
.slide-enter {
  transform: translateX(100%);
}
.slide-leave-to {
  transform: translateX(-100%);
}
</style>

使用 Vue 手势库(如 Hammer.js)

Hammer.js 是一个手势库,可以识别滑动事件。

安装 Hammer.js:

npm install hammerjs

使用示例:

<template>
  <div ref="slider" class="slider">
    <div class="slide">Slide 1</div>
    <div class="slide">Slide 2</div>
    <div class="slide">Slide 3</div>
  </div>
</template>

<script>
import Hammer from 'hammerjs';

export default {
  mounted() {
    const slider = this.$refs.slider;
    const hammer = new Hammer(slider);
    hammer.on('swipeleft', () => {
      console.log('Swiped left');
    });
    hammer.on('swiperight', () => {
      console.log('Swiped right');
    });
  },
};
</script>

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

以上方法可以根据需求选择,CSS 和 Vue 事件适合简单场景,Swiper 提供丰富功能,Vue 动画过渡适合动态内容,Hammer.js 适合复杂手势交互。

vue实现页面左右滑动

标签: 页面vue
分享给朋友:

相关文章

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…