当前位置:首页 > VUE

vue实现京东轮播图

2026-01-21 05:13:48VUE

Vue 实现京东轮播图

使用 vue-awesome-swiper 插件

安装 vue-awesome-swiper 插件,该插件基于 Swiper.js,适合 Vue 项目中使用。

npm install swiper vue-awesome-swiper --save

在组件中引入并使用:

vue实现京东轮播图

<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide v-for="(item, index) in banners" :key="index">
      <img :src="item.image" alt="轮播图" />
    </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 {
      banners: [
        { image: 'https://img10.360buyimg.com/babel/s800x360_jfs/t1/123258/21/5943/136401/5efbf328E8d5b78ef/3d2c7f9e7b7d9b3c.jpg' },
        { image: 'https://img10.360buyimg.com/babel/s800x360_jfs/t1/116056/7/15914/101482/5f4a7a0aEe8b7a7c0/7d0a4c1c1c1c1c1c.jpg' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true
      }
    }
  }
}
</script>

<style scoped>
.swiper-container {
  width: 100%;
  height: 360px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

自定义实现轮播图

如果不使用第三方插件,可以通过 Vue 的 transition 和定时器实现轮播效果。

vue实现京东轮播图

<template>
  <div class="carousel" @mouseenter="stopAutoPlay" @mouseleave="startAutoPlay">
    <div class="carousel-list" :style="{ transform: `translateX(${-currentIndex * 100}%)` }">
      <div class="carousel-item" v-for="(item, index) in banners" :key="index">
        <img :src="item.image" alt="轮播图" />
      </div>
    </div>
    <div class="carousel-dots">
      <span
        v-for="(item, index) in banners"
        :key="index"
        :class="{ active: currentIndex === index }"
        @click="currentIndex = index"
      ></span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      banners: [
        { image: 'https://img10.360buyimg.com/babel/s800x360_jfs/t1/123258/21/5943/136401/5efbf328E8d5b78ef/3d2c7f9e7b7d9b3c.jpg' },
        { image: 'https://img10.360buyimg.com/babel/s800x360_jfs/t1/116056/7/15914/101482/5f4a7a0aEe8b7a7c0/7d0a4c1c1c1c1c1c.jpg' }
      ],
      currentIndex: 0,
      timer: null
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.currentIndex = (this.currentIndex + 1) % this.banners.length
      }, 3000)
    },
    stopAutoPlay() {
      clearInterval(this.timer)
    }
  }
}
</script>

<style scoped>
.carousel {
  position: relative;
  width: 100%;
  height: 360px;
  overflow: hidden;
}
.carousel-list {
  display: flex;
  transition: transform 0.5s ease;
}
.carousel-item {
  flex-shrink: 0;
  width: 100%;
  height: 360px;
}
.carousel-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.carousel-dots {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
}
.carousel-dots span {
  width: 10px;
  height: 10px;
  margin: 0 5px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}
.carousel-dots span.active {
  background: #f00;
}
</style>

关键点说明

轮播图实现的核心在于自动切换和手动控制。通过定时器实现自动播放,通过鼠标事件暂停和恢复播放。

使用第三方插件可以快速实现复杂功能,如无缝循环、手势滑动等。自定义实现更灵活,适合简单需求。

两种方式都可以实现京东风格的轮播图,选择取决于项目需求和技术偏好。

标签: 京东vue
分享给朋友:

相关文章

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…

vue实现spa

vue实现spa

Vue 实现 SPA(单页应用)的步骤 创建 Vue 项目 使用 Vue CLI 快速初始化一个 Vue 项目,确保安装了 Vue Router 作为路由管理工具。运行以下命令创建项目: vue c…

vue实现穿梭框

vue实现穿梭框

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

vue 实现点击选中

vue 实现点击选中

实现点击选中效果 在Vue中实现点击选中效果可以通过多种方式完成,以下是几种常见的方法: 方法一:使用v-bind和v-on 通过绑定class和监听click事件来实现选中状态切换。 <…

vue实现扫码

vue实现扫码

Vue 实现扫码功能 使用 vue-qrcode-reader 库 安装 vue-qrcode-reader 库: npm install vue-qrcode-reader 在 Vue 组件中引入…