当前位置:首页 > 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 封装,适合快速实现轮播或滑动效果。

安装依赖:

npm install vue-slick-carousel

示例代码:

<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实现该功能需要结合文本分析、关键词提取和前端渲染技术。 文本分析与关键词…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="co…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…