当前位置:首页 > 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. 无限循环优化

    // 替代简单的取模运算
    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 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

前端多线程实现vue

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker…

vue实现下拉框删除

vue实现下拉框删除

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

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些…

jq 实现 vue

jq 实现 vue

jq 实现 Vue 的核心功能 jq 是一个轻量级的 JavaScript 库,主要用于 DOM 操作和事件处理。虽然它不能完全替代 Vue,但可以通过一些技巧模拟 Vue 的核心功能,如数据绑定、事…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…