当前位置:首页 > VUE

vue实现图片滚动

2026-01-16 01:19:20VUE

实现图片滚动的 Vue 组件

使用 Vue 实现图片滚动效果可以通过自定义组件或第三方库完成。以下是两种常见方法:

方法一:使用 CSS 动画和 Vue 动态绑定

通过 Vue 的 v-for 和 CSS 动画实现基础横向滚动:

<template>
  <div class="scroll-container">
    <div class="scroll-content" :style="{ transform: `translateX(${scrollPosition}px)` }">
      <img v-for="(img, index) in images" :key="index" :src="img" class="scroll-img">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ],
      scrollPosition: 0,
      scrollSpeed: 2
    }
  },
  mounted() {
    this.startScrolling()
  },
  methods: {
    startScrolling() {
      setInterval(() => {
        this.scrollPosition -= this.scrollSpeed
        if (Math.abs(this.scrollPosition) >= this.$el.querySelector('.scroll-content').scrollWidth / 2) {
          this.scrollPosition = 0
        }
      }, 30)
    }
  }
}
</script>

<style>
.scroll-container {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
}
.scroll-content {
  display: inline-block;
  transition: transform 0.1s linear;
}
.scroll-img {
  height: 200px;
  margin-right: 10px;
}
</style>

方法二:使用第三方库 vue-awesome-swiper

vue实现图片滚动

安装 Swiper 的 Vue 封装版本:

npm install swiper vue-awesome-swiper

组件实现代码:

vue实现图片滚动

<template>
  <swiper :options="swiperOption">
    <swiper-slide v-for="(img, index) in images" :key="index">
      <img :src="img" class="slide-img">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      images: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination'
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

<style>
.slide-img {
  width: 100%;
  height: auto;
}
</style>

关键实现要点

无缝循环处理

  • 方法一中通过重置 scrollPosition 实现无限循环
  • 方法二通过 Swiper 的 loop 配置自动处理

性能优化

  • 对于大量图片建议使用懒加载
  • 适当限制动画帧率避免过度渲染
  • 移动端注意 touch 事件支持

扩展功能

  • 添加导航按钮控制滚动
  • 实现响应式图片尺寸
  • 支持垂直滚动方向
  • 添加鼠标悬停暂停功能

两种方法各有优势:纯 CSS 方案更轻量但功能有限;Swiper 方案功能丰富但会增加包体积。根据项目需求选择合适方案即可。

标签: 图片vue
分享给朋友:

相关文章

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue实现日历

vue实现日历

Vue 实现日历的基本方法 使用 Vue 实现日历可以通过自定义组件或借助第三方库完成。以下是两种常见实现方式: 自定义日历组件 创建基于 Vue 的日历组件需要处理日期计算和渲染逻辑。核心是生成当…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现穿梭框

vue实现穿梭框

Vue 穿梭框实现方法 穿梭框(Transfer)是一种常见的UI组件,用于在两个列表之间移动数据项。以下是基于Vue的实现方法。 基础结构搭建 创建两个列表容器和一个操作按钮区域,使用v-mode…

vue实现点击跳转

vue实现点击跳转

Vue 实现点击跳转的方法 在 Vue 中实现点击跳转可以通过以下几种方式: 使用 router-link router-link 是 Vue Router 提供的组件,用于声明式导航。适合在模板中…