当前位置:首页 > 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实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vue…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成…

vue实现下拉框删除

vue实现下拉框删除

Vue 实现下拉框删除功能 在Vue中实现下拉框的删除功能通常涉及以下步骤: 数据绑定与渲染 在Vue组件中定义数据数组用于存储下拉框选项: data() { return {…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…