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

在组件中引入并使用:

<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 和定时器实现轮播效果。

<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中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli v…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 push…