当前位置:首页 > VUE

vue 实现div横向滚动

2026-02-20 15:12:56VUE

实现横向滚动的基本方法

在Vue中实现div横向滚动,可以通过CSS样式控制容器的宽度和溢出行为。横向滚动的关键在于设置white-space: nowrapoverflow-x: auto

<template>
  <div class="horizontal-scroll-container">
    <div class="scroll-content">
      <div v-for="item in items" :key="item.id" class="scroll-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' },
        // 更多项目...
      ]
    }
  }
}
</script>

<style>
.horizontal-scroll-container {
  width: 100%;
  overflow-x: auto;
}

.scroll-content {
  display: inline-block;
  white-space: nowrap;
}

.scroll-item {
  display: inline-block;
  width: 200px;
  height: 100px;
  margin-right: 10px;
  background-color: #f0f0f0;
}
</style>

使用Flexbox布局

Flexbox提供了更现代的横向滚动实现方式,通过设置flex-direction: rowflex-wrap: nowrap来达到效果。

vue 实现div横向滚动

.horizontal-scroll-container {
  display: flex;
  overflow-x: auto;
  width: 100%;
}

.scroll-content {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
}

.scroll-item {
  flex: 0 0 auto;
  width: 200px;
  height: 100px;
  margin-right: 10px;
}

响应式设计考虑

为了在不同屏幕尺寸下保持良好的用户体验,可以添加响应式设计:

vue 实现div横向滚动

.scroll-item {
  min-width: 150px;
  width: calc(25% - 10px);
  margin-right: 10px;
}

@media (max-width: 768px) {
  .scroll-item {
    min-width: 120px;
    width: calc(33.333% - 10px);
  }
}

@media (max-width: 480px) {
  .scroll-item {
    min-width: 100px;
    width: calc(50% - 10px);
  }
}

隐藏滚动条

如果需要隐藏滚动条但仍保留滚动功能,可以使用以下CSS:

.horizontal-scroll-container {
  -ms-overflow-style: none;  /* IE and Edge */
  scrollbar-width: none;  /* Firefox */
}

.horizontal-scroll-container::-webkit-scrollbar {
  display: none;  /* Chrome, Safari, Opera */
}

动态加载内容

对于大量数据,可以结合虚拟滚动技术优化性能:

<template>
  <div ref="scrollContainer" class="horizontal-scroll-container" @scroll="handleScroll">
    <div class="scroll-content" :style="{ width: totalWidth + 'px' }">
      <div v-for="item in visibleItems" :key="item.id" class="scroll-item">
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allItems: [], // 所有数据
      visibleItems: [], // 当前可见数据
      itemWidth: 200,
      visibleCount: 0
    }
  },
  computed: {
    totalWidth() {
      return this.allItems.length * this.itemWidth
    }
  },
  mounted() {
    this.calculateVisibleCount()
    window.addEventListener('resize', this.calculateVisibleCount)
    this.updateVisibleItems()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateVisibleCount)
  },
  methods: {
    calculateVisibleCount() {
      const containerWidth = this.$refs.scrollContainer.clientWidth
      this.visibleCount = Math.ceil(containerWidth / this.itemWidth) + 2
    },
    handleScroll() {
      this.updateVisibleItems()
    },
    updateVisibleItems() {
      const scrollLeft = this.$refs.scrollContainer.scrollLeft
      const startIndex = Math.floor(scrollLeft / this.itemWidth)
      this.visibleItems = this.allItems.slice(
        startIndex,
        startIndex + this.visibleCount
      )
    }
  }
}
</script>

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

相关文章

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.definePropert…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…