当前位置:首页 > VUE

vue实现图片横向滚动

2026-02-23 16:40:15VUE

实现图片横向滚动的 Vue 方法

使用 CSS Flex 布局和 overflow

通过 CSS Flex 布局结合 overflow-x: auto 实现横向滚动效果。适用于简单的图片列表横向滚动需求。

<template>
  <div class="scroll-container">
    <div class="image-list">
      <img 
        v-for="(image, index) in images" 
        :key="index" 
        :src="image" 
        class="image-item"
      />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'path/to/image1.jpg',
        'path/to/image2.jpg',
        'path/to/image3.jpg',
        // 更多图片...
      ]
    }
  }
}
</script>

<style scoped>
.scroll-container {
  width: 100%;
  overflow-x: auto;
  white-space: nowrap;
}

.image-list {
  display: flex;
  gap: 10px; /* 图片间距 */
  padding: 10px 0;
}

.image-item {
  width: 200px; /* 固定宽度 */
  height: 150px; /* 固定高度 */
  object-fit: cover;
  flex-shrink: 0; /* 防止图片缩小 */
}
</style>

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

对于更复杂的横向滚动需求(如分页、响应式控制),可以使用专用库如 vue-horizontal

安装依赖:

npm install vue-horizontal

示例代码:

vue实现图片横向滚动

<template>
  <vue-horizontal>
    <img 
      v-for="(image, index) in images" 
      :key="index" 
      :src="image" 
      class="image"
    />
  </vue-horizontal>
</template>

<script>
import VueHorizontal from "vue-horizontal";

export default {
  components: { VueHorizontal },
  data() {
    return {
      images: [
        'path/to/image1.jpg',
        'path/to/image2.jpg',
        // 更多图片...
      ]
    }
  }
}
</script>

<style>
.image {
  width: 300px;
  height: 200px;
  object-fit: cover;
  margin-right: 16px;
}
</style>

自定义滚动按钮控制

通过 ref 操作 DOM 实现手动控制滚动,适合需要自定义按钮交互的场景。

<template>
  <div class="scroll-wrapper">
    <button @click="scroll(-100)">←</button>
    <div class="scroll-container" ref="scrollContainer">
      <div class="image-list">
        <img 
          v-for="(image, index) in images" 
          :key="index" 
          :src="image" 
          class="image-item"
        />
      </div>
    </div>
    <button @click="scroll(100)">→</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [/* 图片数组 */]
    }
  },
  methods: {
    scroll(offset) {
      this.$refs.scrollContainer.scrollBy({
        left: offset,
        behavior: 'smooth'
      });
    }
  }
}
</script>

<style>
.scroll-wrapper {
  display: flex;
  align-items: center;
}

.scroll-container {
  width: 80%;
  overflow-x: auto;
  scroll-behavior: smooth;
}

.image-list {
  display: flex;
  gap: 15px;
  padding: 10px;
}

.image-item {
  width: 180px;
  height: 120px;
  object-fit: cover;
}
</style>

响应式设计建议

添加媒体查询适应不同屏幕尺寸:

vue实现图片横向滚动

@media (max-width: 768px) {
  .image-item {
    width: 120px;
    height: 90px;
  }
}

性能优化提示

  1. 对大量图片使用懒加载:

    <img loading="lazy" ... />
  2. 使用 IntersectionObserver 实现动态加载:

    
    // 在 mounted 中观察图片可见性
    mounted() {
    const observer = new IntersectionObserver((entries) => {
     entries.forEach(entry => {
       if (entry.isIntersecting) {
         entry.target.src = entry.target.dataset.src;
         observer.unobserve(entry.target);
       }
     });
    });

document.querySelectorAll('img[data-src]').forEach(img => { observer.observe(img); }); }

标签: 横向图片
分享给朋友:

相关文章

vue实现图片预览

vue实现图片预览

实现图片预览功能 在Vue中实现图片预览功能可以通过多种方式完成,常见的有使用原生HTML5的<dialog>元素、第三方库如viewer.js或vue-photo-preview,以及自…

vue实现展示图片

vue实现展示图片

在 Vue 中展示图片的方法 静态图片展示 静态图片通常存放在项目的 public 或 assets 目录下。使用 img 标签直接引用。 <template> <img sr…

js 实现图片轮播

js 实现图片轮播

基础实现方案 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS负责样式布局,JavaScript处理轮播逻辑。 <div class="…

jquery图片轮播

jquery图片轮播

jQuery图片轮播实现方法 基础HTML结构 创建一个简单的HTML结构,包含轮播容器、图片列表和导航按钮。 <div class="slider"> <div class=…

vue实现生成图片

vue实现生成图片

Vue 实现生成图片的方法 使用 html2canvas 库 html2canvas 是一个流行的库,可以将 DOM 元素转换为 Canvas,进而生成图片。适用于 Vue 项目。 安装 html2…

vue 实现 图片预览

vue 实现 图片预览

实现图片预览功能 在Vue中实现图片预览功能可以通过多种方式完成,以下是几种常见的方法: 使用HTML5的FileReader API 通过FileReader读取用户上传的图片文件并显示预览:…