当前位置:首页 > VUE

vue实现横向点击滑动

2026-02-22 19:59:43VUE

Vue 实现横向点击滑动

使用 CSS 和 Vue 事件绑定

通过 CSS 的 overflow-xscroll-behavior 属性实现横向滑动容器,结合 Vue 的 @click 事件触发滚动。

<template>
  <div class="scroll-container" ref="scrollContainer">
    <div class="scroll-content">
      <div v-for="item in items" :key="item.id" class="item">{{ item.text }}</div>
    </div>
    <button @click="scrollLeft">左滑</button>
    <button @click="scrollRight">右滑</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: "Item 1" },
        { id: 2, text: "Item 2" },
        // 更多项目...
      ]
    };
  },
  methods: {
    scrollLeft() {
      this.$refs.scrollContainer.scrollBy({ left: -200, behavior: 'smooth' });
    },
    scrollRight() {
      this.$refs.scrollContainer.scrollBy({ left: 200, behavior: 'smooth' });
    }
  }
};
</script>

<style>
.scroll-container {
  width: 100%;
  overflow-x: auto;
  scroll-behavior: smooth;
  white-space: nowrap;
}
.scroll-content {
  display: inline-flex;
}
.item {
  width: 200px;
  height: 100px;
  margin: 0 10px;
  background: #eee;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
</style>

使用第三方库(如 vue-slick)

vue-slick 是基于 Slick Carousel 的 Vue 封装,适合快速实现轮播或滑动效果。

vue实现横向点击滑动

安装依赖:

npm install vue-slick-carousel

示例代码:

vue实现横向点击滑动

<template>
  <vue-slick ref="slick" :options="slickOptions">
    <div v-for="item in items" :key="item.id">
      {{ item.text }}
    </div>
  </vue-slick>
</template>

<script>
import VueSlick from 'vue-slick-carousel';

export default {
  components: { VueSlick },
  data() {
    return {
      items: [
        { id: 1, text: "Item 1" },
        { id: 2, text: "Item 2" },
      ],
      slickOptions: {
        slidesToShow: 3,
        arrows: true,
        dots: true,
      }
    };
  }
};
</script>

动态计算滑动距离

通过计算容器宽度和项目宽度,动态调整每次滑动的距离。

methods: {
  scrollRight() {
    const container = this.$refs.scrollContainer;
    const itemWidth = 200; // 单个项目宽度
    container.scrollBy({ left: itemWidth, behavior: 'smooth' });
  }
}

响应式滑动

结合 ResizeObserver 监听容器宽度变化,动态调整滑动逻辑。

mounted() {
  const observer = new ResizeObserver(entries => {
    entries.forEach(entry => {
      this.containerWidth = entry.contentRect.width;
    });
  });
  observer.observe(this.$refs.scrollContainer);
}

注意事项

  • 移动端兼容性:确保添加 touch-action CSS 属性以支持触摸滑动。
  • 性能优化:对于大量数据,建议使用虚拟滚动(如 vue-virtual-scroller)。
  • 浏览器支持:scroll-behavior: smooth 在旧版本浏览器中可能需要 polyfill。

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

相关文章

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…