当前位置:首页 > 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

示例代码:

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

响应式设计建议

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

@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实现图片横向滚动

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

相关文章

网页设计制作css图片

网页设计制作css图片

CSS图片处理基础方法 在网页设计中,CSS可以控制图片的尺寸、位置、边框等样式属性。通过<img>标签或背景图方式引入图片后,使用CSS属性进行调整。 img { width: 1…

css3手工制作图片

css3手工制作图片

使用CSS3手工制作图片 CSS3可以通过各种属性如box-shadow、border-radius、gradient等来手工绘制简单的图形或图片效果。以下是几种常见的方法: 绘制圆形 通过bor…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-c…

vue实现图片单选

vue实现图片单选

Vue 实现图片单选功能 在 Vue 中实现图片单选功能可以通过多种方式完成,以下是两种常见的实现方法: 方法一:使用 v-model 和单选按钮 通过将单选按钮与图片绑定,利用 v-model…

react如何引用图片

react如何引用图片

在React中引用图片的方法 使用import直接引入图片 在React组件中,可以通过import语句将图片作为模块引入。这种方式适合在组件内部使用图片,Webpack会处理图片的路径和打包。…

vue实现图片滚动

vue实现图片滚动

实现图片滚动的 Vue 组件 使用 Vue 实现图片滚动效果可以通过自定义组件或第三方库完成。以下是两种常见方法: 方法一:使用 CSS 动画和 Vue 动态绑定 通过 Vue 的 v-for 和…