当前位置:首页 > 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实现tab切换

vue实现tab切换

Vue实现Tab切换的方法 使用v-if和v-show指令 通过v-if或v-show控制不同tab内容的显示与隐藏。v-if会销毁和重建DOM,适合切换频率低的场景;v-show仅切换CSS的dis…

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router…

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const route…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…