当前位置:首页 > VUE

vue实现小米轮播图

2026-02-24 16:39:19VUE

Vue实现小米轮播图

使用Vue实现类似小米官网的轮播图效果,可以通过以下步骤完成。示例基于Vue 3的Composition API和<script setup>语法。

核心实现思路

  1. 数据驱动:通过数组存储轮播图数据(图片URL、跳转链接等)。
  2. 自动轮播:使用setInterval实现定时切换。
  3. 无缝衔接:通过复制首尾图片实现视觉无缝效果。
  4. 手势支持:添加touchstart/touchmove事件实现滑动切换。

完整代码示例

<template>
  <div class="slider-container" 
       @mouseenter="pauseAutoPlay" 
       @mouseleave="startAutoPlay">
    <div class="slider-track" 
         :style="{ transform: `translateX(${-currentIndex * 100}%)` }"
         @touchstart="handleTouchStart"
         @touchmove="handleTouchMove"
         @touchend="handleTouchEnd">
      <div v-for="(item, index) in slides" :key="index" class="slide">
        <img :src="item.image" :alt="item.title">
      </div>
    </div>
    <div class="slider-dots">
      <span v-for="(dot, index) in realSlides.length" 
            :key="index" 
            :class="{ active: currentIndex === index }"
            @click="goToSlide(index)"></span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

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

// 克隆首尾图片实现无缝滚动
const realSlides = ref([...slides.value]);
realSlides.value.unshift(slides.value[slides.value.length - 1]);
realSlides.value.push(slides.value[0]);

const currentIndex = ref(1);
let autoPlayTimer = null;
let touchStartX = 0;
let touchEndX = 0;

const startAutoPlay = () => {
  autoPlayTimer = setInterval(() => {
    nextSlide();
  }, 3000);
};

const pauseAutoPlay = () => {
  clearInterval(autoPlayTimer);
};

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % realSlides.value.length;
  if (currentIndex.value === realSlides.value.length - 1) {
    setTimeout(() => {
      currentIndex.value = 1;
    }, 500);
  }
};

const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + realSlides.value.length) % realSlides.value.length;
  if (currentIndex.value === 0) {
    setTimeout(() => {
      currentIndex.value = realSlides.value.length - 2;
    }, 500);
  }
};

const goToSlide = (index) => {
  currentIndex.value = index + 1;
};

const handleTouchStart = (e) => {
  touchStartX = e.touches[0].clientX;
  pauseAutoPlay();
};

const handleTouchMove = (e) => {
  touchEndX = e.touches[0].clientX;
};

const handleTouchEnd = () => {
  if (touchStartX - touchEndX > 50) {
    nextSlide();
  } else if (touchEndX - touchStartX > 50) {
    prevSlide();
  }
  startAutoPlay();
};

onMounted(() => {
  startAutoPlay();
});

onUnmounted(() => {
  pauseAutoPlay();
});
</script>

<style scoped>
.slider-container {
  position: relative;
  width: 100%;
  height: 400px;
  overflow: hidden;
}

.slider-track {
  display: flex;
  height: 100%;
  transition: transform 0.5s ease;
}

.slide {
  flex: 0 0 100%;
}

.slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.slider-dots {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.slider-dots span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
}

.slider-dots span.active {
  background: #fff;
}
</style>

关键实现细节

无缝滚动技术

  • 在数组首尾分别添加克隆的最后一幅和第一幅图片
  • 当滚动到克隆图片时,通过setTimeout立即跳转到真实图片位置

自动轮播控制

  • onMounted时启动定时器,onUnmounted时清除
  • 鼠标悬停时暂停轮播,离开时恢复

触摸事件处理

  • 记录触摸起始和结束位置的X坐标
  • 根据滑动距离判断切换方向(左滑下一张/右滑上一张)

进阶优化建议

  1. 响应式适配

    @media (max-width: 768px) {
    .slider-container {
     height: 200px;
    }
    }
  2. 懒加载图片

    <img v-lazy="item.image" :alt="item.title">
  3. 过渡动画增强

    .slider-track {
    transition: transform 0.5s cubic-bezier(0.25, 0.1, 0.25, 1);
    }
  4. 无限循环优化

    vue实现小米轮播图

    // 替代简单的取模运算
    if (currentIndex.value >= realSlides.value.length - 1) {
    currentIndex.value = 1;
    }
    if (currentIndex.value <= 0) {
    currentIndex.value = realSlides.value.length - 2;
    }

注意事项

  1. 图片路径需替换为实际项目资源
  2. 移动端需添加touch-action: pan-y样式防止浏览器默认行为干扰
  3. 对于Vue 2版本,需将Composition API改为Options API写法

标签: 小米vue
分享给朋友:

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 在 Vue 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现: &l…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

vue实现双击

vue实现双击

Vue 实现双击事件的方法 在Vue中实现双击事件可以通过以下几种方式完成,具体选择取决于项目需求和开发习惯。 使用 @dblclick 指令 Vue提供了内置的@dblclick指令,可以直接在模…