当前位置:首页 > VUE

vue实现横向点击滑动

2026-01-22 04:57:58VUE

Vue 实现横向点击滑动

使用 CSS 和 Vue 事件绑定

通过 Vue 的 @click 事件绑定和 CSS 的 transform 属性实现横向滑动效果。
定义滑动距离变量,点击按钮时修改该变量,通过计算属性动态绑定样式。

<template>
  <div class="slider-container">
    <button @click="slide(-100)">向左滑动</button>
    <div class="slider" :style="{ transform: `translateX(${offset}px)` }">
      <div v-for="(item, index) in items" :key="index" class="slide-item">
        {{ item }}
      </div>
    </div>
    <button @click="slide(100)">向右滑动</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      offset: 0,
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
    };
  },
  methods: {
    slide(distance) {
      this.offset += distance;
    }
  }
};
</script>

<style>
.slider-container {
  overflow: hidden;
  position: relative;
}
.slider {
  display: flex;
  transition: transform 0.3s ease;
}
.slide-item {
  flex: 0 0 100px;
  height: 100px;
  margin: 0 10px;
  background: #eee;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

使用第三方库(如 Swiper)

对于更复杂的滑动需求,可以使用 Swiper 库,它提供了丰富的滑动效果和 API。
安装 Swiper 并集成到 Vue 项目中。

vue实现横向点击滑动

npm install swiper

在 Vue 组件中使用 Swiper:

vue实现横向点击滑动

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(item, index) in items" :key="index">
      {{ item }}
    </swiper-slide>
  </swiper>
</template>

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

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

动态计算滑动距离

根据容器宽度和子元素数量动态计算滑动距离,确保每次滑动一个完整元素。
使用 clientWidth 获取元素宽度,动态调整 offset 值。

<template>
  <div class="slider-container" ref="container">
    <button @click="slide('left')">向左滑动</button>
    <div class="slider" :style="{ transform: `translateX(${offset}px)` }" ref="slider">
      <div v-for="(item, index) in items" :key="index" class="slide-item" ref="items">
        {{ item }}
      </div>
    </div>
    <button @click="slide('right')">向右滑动</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      offset: 0,
      items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
      itemWidth: 0
    };
  },
  mounted() {
    this.itemWidth = this.$refs.items[0].clientWidth + 20; // 包括 margin
  },
  methods: {
    slide(direction) {
      const containerWidth = this.$refs.container.clientWidth;
      const maxOffset = -(this.items.length * this.itemWidth - containerWidth);

      if (direction === 'left' && this.offset < 0) {
        this.offset += this.itemWidth;
      } else if (direction === 'right' && this.offset > maxOffset) {
        this.offset -= this.itemWidth;
      }
    }
  }
};
</script>

响应式滑动

结合 Vue 的响应式特性,监听窗口大小变化,动态调整滑动逻辑。
使用 window.addEventListener 监听 resize 事件,更新滑动参数。

mounted() {
  window.addEventListener('resize', this.handleResize);
  this.handleResize();
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.itemWidth = this.$refs.items[0].clientWidth + 20;
  }
}

标签: 横向vue
分享给朋友:

相关文章

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…