当前位置:首页 > VUE

vue实现京东轮播图

2026-02-21 20:45:05VUE

Vue 实现京东轮播图

京东轮播图通常包含自动轮播、手动滑动、指示器导航等功能。以下是基于 Vue 3 的实现方案,结合 swiper 库(业界常用轮播组件)完成。

安装 Swiper 依赖

确保项目已安装 swipervue-awesome-swiper(Vue 封装版本):

vue实现京东轮播图

npm install swiper vue-awesome-swiper

基础轮播图实现

创建一个基础轮播组件,支持自动播放和指示器:

<template>
  <swiper
    :modules="[Autoplay, Pagination]"
    :slides-per-view="1"
    :space-between="20"
    :autoplay="{ delay: 3000 }"
    :pagination="{ clickable: true }"
    @swiper="onSwiper"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" class="w-full h-auto" />
    </swiper-slide>
  </swiper>
</template>

<script setup>
import { ref } from 'vue';
import { Autoplay, Pagination } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/pagination';

const slides = ref([
  { image: 'https://via.placeholder.com/800x400?text=Slide1' },
  { image: 'https://via.placeholder.com/800x400?text=Slide2' },
  { image: 'https://via.placeholder.com/800x400?text=Slide3' },
]);

const onSwiper = (swiper) => {
  console.log('Swiper initialized:', swiper);
};
</script>

添加手动滑动效果

通过 Navigation 模块添加左右箭头按钮:

vue实现京东轮播图

<template>
  <swiper
    :modules="[Autoplay, Pagination, Navigation]"
    :navigation="{ nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' }"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" />
    </swiper-slide>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </swiper>
</template>

<script setup>
import { Navigation } from 'swiper/modules';
import 'swiper/css/navigation';
</script>

响应式适配

根据屏幕宽度调整显示数量(如京东首页的多图轮播):

const breakpoints = {
  640: { slidesPerView: 2 },
  1024: { slidesPerView: 3 },
};

样式优化

覆盖默认样式以匹配京东风格:

.swiper-pagination-bullet-active {
  background-color: #f10215; /* 京东红 */
}
.swiper-button-next, .swiper-button-prev {
  color: #f10215;
}

完整示例代码

<template>
  <div class="relative">
    <swiper
      :modules="[Autoplay, Pagination, Navigation]"
      :slides-per-view="1"
      :space-between="20"
      :autoplay="{ delay: 3000, disableOnInteraction: false }"
      :pagination="{ clickable: true }"
      :navigation="{ nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' }"
      :breakpoints="breakpoints"
    >
      <swiper-slide v-for="(slide, index) in slides" :key="index">
        <img :src="slide.image" class="w-full" />
      </swiper-slide>
      <div class="swiper-button-next"></div>
      <div class="swiper-button-prev"></div>
    </swiper>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Autoplay, Pagination, Navigation } from 'swiper/modules';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/pagination';
import 'swiper/css/navigation';

const slides = ref([
  { image: 'https://via.placeholder.com/1200x400?text=JD+Slide1' },
  { image: 'https://via.placeholder.com/1200x400?text=JD+Slide2' },
  { image: 'https://via.placeholder.com/1200x400?text=JD+Slide3' },
]);

const breakpoints = {
  768: { slidesPerView: 1.5 },
  1024: { slidesPerView: 2 },
};
</script>

<style scoped>
.swiper-pagination-bullet-active {
  background: #f10215;
}
.swiper-button-next:after, .swiper-button-prev:after {
  font-size: 24px;
  color: #f10215;
}
</style>

关键点说明

  • 自动播放:通过 autoplay 参数控制间隔时间。
  • 无缝循环:添加 loop: true 参数可实现无限循环。
  • 触摸交互:默认支持手指滑动切换。
  • 性能优化:懒加载图片可使用 lazy: true 配置。

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

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…