纯VUE实现手写轮播图

本文分享了使用VUE实现一个简单轮播图的过程。思路与原生JS类似,通过设置父元素样式并利用transition和transform完成轮播效果。文章包括实现思路、效果图及代码展示,适合前端初学者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        使用VUE也有一段时间了,今天在工作过程中用VUE手写了一个简单的VUE轮播图,在这里分享一下。

        (一)实现思路:

                和原生JS的实现思路一样只不过是代码展现方式有所不同,同样是给外侧父盒子限定宽度和高度,之后添加overflow: hidden;属性。然后在内部展示轮播内容,利用transition以及transfrom实现轮播。

        (二)效果图

         (三)代码

<template>
    <div class="Tuan_box">
      <div class="rule_Box">
        <p class="rule_title">拼团规则<i class="iconfont icon-wenhao"></i></p>
        <p class="rule_num">9999人正在拼团</p>
      </div>
      <div class="Tuan_swiper_box" ref="swiper_box" :style="swiperStyle">
        <div
          @mouseleave="swiperplay()"
          @mouseenter="swiperStop()"
          class="swiper_item"
          v-for="(item, index) in 10"
          :key="index"
        >
          <div class="headImag_box">
            <img
              src="https://www.jszg.com/static/platform/pic/img_headpic.png"
              alt=""
            />
          </div>
          <div class="Tuan_name">188****8888</div>
          <div class="right_btn">
            <div class="Tuan_info">
              <p class="Tuan_info_top">还差99人拼成</p>
              <p class="Tuan_info_bottom">剩余23:23:23</p>
            </div>
  &nbs
好的,下面是一个简单的手写轮播图组件的实现: ```html <template> <div class="carousel"> <!-- 可视区域 --> <div class="viewport"> <div class="slider" :style="{ transform: `translateX(${translateX}px)` }"> <div class="slide" v-for="(item, index) in items" :key="index"> <img :src="item.imgUrl" alt=""> </div> </div> </div> <!-- 控制按钮 --> <div class="buttons"> <button class="prev" @click="prev"><</button> <button class="next" @click="next">></button> </div> <!-- 指示器 --> <div class="indicators"> <span class="indicator" v-for="(item, index) in items" :key="index" :class="{ active: index === currentIndex }" @click="jumpTo(index)"></span> </div> </div> </template> <script> export default { name: 'Carousel', props: { items: { type: Array, default: () => [] } }, data() { return { currentIndex: 0, // 当前显示的图片索引 translateX: 0 // slider 的 translateX 值 } }, methods: { // 切换到上一张图片 prev() { if (this.currentIndex > 0) { this.currentIndex--; this.translateX += this.slideWidth; } else { this.currentIndex = this.items.length - 1; this.translateX = -this.slideWidth * (this.items.length - 1); } }, // 切换到下一张图片 next() { if (this.currentIndex < this.items.length - 1) { this.currentIndex++; this.translateX -= this.slideWidth; } else { this.currentIndex = 0; this.translateX = 0; } }, // 跳转到指定图片 jumpTo(index) { this.currentIndex = index; this.translateX = -this.slideWidth * index; } }, computed: { slideWidth() { return this.$refs.slider.offsetWidth; } } } </script> <style scoped> .carousel { position: relative; width: 100%; overflow: hidden; } .viewport { position: relative; overflow: hidden; } .slider { display: flex; transition: transform 0.3s ease-out; } .slide { flex-shrink: 0; width: 100%; text-align: center; } .buttons { position: absolute; top: 50%; transform: translateY(-50%); z-index: 1; } .buttons button { font-size: 2rem; color: #fff; background-color: rgba(0, 0, 0, 0.5); border: none; outline: none; cursor: pointer; padding: 0.5rem; margin: 0 1rem; } .indicators { position: absolute; bottom: 1rem; left: 50%; transform: translateX(-50%); z-index: 1; display: flex; } .indicator { width: 1rem; height: 1rem; border-radius: 50%; background-color: #fff; margin: 0 0.5rem; cursor: pointer; } .indicator.active { background-color: #f00; } </style> ``` 这个组件的主要思路是利用 flex 布局和 translateX 属性实现图片的滑动。其中,控制按钮和指示器使用了绝对定位的方式进行布局。具体实现过程中,我们需要获取 slider 的宽度,以便计算出 translateX 的值。同时,我们还需要在切换到第一张或最后一张图片时,调整 translateX 的值,以实现循环滑动的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值