当前位置:首页 > VUE

vue 实现div横向滚动

2026-01-19 23:08:14VUE

实现横向滚动的基本方法

在Vue中实现div横向滚动可以通过CSS的overflow-x属性结合white-space或Flexbox布局完成。以下是两种常见实现方式:

方法一:使用inline-block元素

vue 实现div横向滚动

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

<style scoped>
.scroll-container {
  overflow-x: auto;
  white-space: nowrap;
  width: 100%;
  -webkit-overflow-scrolling: touch; /* 优化iOS滚动 */
}
.scroll-item {
  display: inline-block;
  width: 200px;
  height: 100px;
  margin-right: 10px;
}
</style>

方法二:使用Flexbox布局

vue 实现div横向滚动

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

<style scoped>
.scroll-container {
  overflow-x: auto;
  width: 100%;
}
.scroll-wrapper {
  display: flex;
  flex-wrap: nowrap;
  padding: 10px 0;
}
.scroll-item {
  flex: 0 0 auto;
  width: 200px;
  margin-right: 10px;
}
</style>

添加滚动阴影效果

为增强用户体验,可以通过伪元素添加滚动边界提示:

.scroll-container {
  position: relative;
}
.scroll-container::after {
  content: "";
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 30px;
  background: linear-gradient(to right, transparent, rgba(0,0,0,0.1));
  pointer-events: none;
}

响应式处理

结合Vue的响应式特性,动态计算容器宽度:

export default {
  data() {
    return {
      items: [...],
      containerWidth: 0
    }
  },
  mounted() {
    this.updateWidth()
    window.addEventListener('resize', this.updateWidth)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.updateWidth)
  },
  methods: {
    updateWidth() {
      this.containerWidth = this.$el.offsetWidth
    }
  }
}

性能优化建议

  • 对大量滚动元素使用v-show替代v-if避免重复渲染
  • 添加will-change: transform提升滚动性能
  • 使用Intersection Observer API实现懒加载
  • 移动端可添加-webkit-overflow-scrolling: touch启用弹性滚动

完整组件示例

<template>
  <div ref="container" class="horizontal-scroll">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HorizontalScroll',
  mounted() {
    this.initScroll()
  },
  methods: {
    initScroll() {
      const container = this.$refs.container
      container.addEventListener('wheel', (e) => {
        if (e.deltaY !== 0) {
          e.preventDefault()
          container.scrollLeft += e.deltaY
        }
      }, { passive: false })
    }
  }
}
</script>

<style scoped>
.horizontal-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.horizontal-scroll::-webkit-scrollbar {
  display: none;
}
.content {
  display: inline-flex;
  min-width: 100%;
}
</style>

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

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: c…