当前位置:首页 > VUE

vue 实现div横向滚动

2026-01-19 23:08:14VUE

实现横向滚动的基本方法

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

方法一:使用inline-block元素

<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布局

<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的响应式特性,动态计算容器宽度:

vue 实现div横向滚动

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 CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue实现登录权限

vue实现登录权限

Vue 实现登录权限方案 路由守卫控制访问权限 安装vue-router并配置路由守卫,在全局前置守卫中检查用户登录状态和权限。未登录用户访问受限路由时重定向到登录页。 // router.js r…

vue实现接口连接

vue实现接口连接

Vue 实现接口连接的常用方法 Vue 中实现接口连接通常需要借助 HTTP 客户端库,以下是几种常见实现方式: 使用 Axios Axios 是流行的 HTTP 客户端库,支持 Promise A…