vue3封装轮播图组件的方法

目的

封装轮播图组件,直接使用,具体内容如下

大致步骤

  • 准备my-carousel组件基础布局,全局注册
  • 准备home-banner组件,使用my-carousel组件,再首页注册使用。
  • 深度作用选择器覆盖my-carousel组件的默认样式
  • 在home-banner组件获取轮播图数据,传递给my-carousel组件
  • 在my-carousel组件完成渲染
  • 自动播放,暴露自动轮播属性,设置了就自动轮播
  • 如果有自动播放,鼠标进入离开,暂停,开启
  • 指示器切换,上一张,下一张
  • 销毁组件,清理定时器

落地代码

一、封装组件

<template>
  <div class="my-carousel" @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li v-for="(item, i) in findBannerList" :key="item.id" class="carousel-item" :class="{ fade: index === i }">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="图片" />
        </RouterLink>
      </li>
    </ul>
    <a @click="clickFn(-1)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="clickFn(1)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span @click="active(i)" v-for="(item, i) in findBannerList" :key="i" :class="{ active: index === i }"></span>
    </div>
  </div>
</template>

<script>
import { onUnmounted, ref, watch } from 'vue'
export default {
  name: 'Carousel',
  props: {
    findBannerList: {
      type: Array,
      default: () => []
    },
    autoplay: {
      type: Boolean,
      default: true
    },
    duration: {
      type: Number,
      default: 3
    }
  },
  setup(props) {
    const index = ref(0)
    // 定义一个常量存储定时器
    const timer = ref(null)
    // 定时器方法,实现自动轮播效果
    const autoplayFn = () => {
      // 防抖,防止多次触发定时器
      clearInterval(timer.value)
      timer.value = setInterval(() => {
        index.value += 1
        if (index.value >= props.findBannerList.length) {
          index.value = 0
        }
      }, props.duration * 1000)
    }
    // 侦听器,根据接口返回的数据与传递的相关属性参数 autoplay 开启轮播
    // 监听返回的数据的长度,当长度大于1的时候并且 autoplay 的为 true 的时候开启轮播
    watch(
      () => props.findBannerList,
      () => {
        if (props.findBannerList.length > 1 && props.autoplay) {
          autoplayFn()
        }
      }
    )
    // 鼠标移入轮播图,停止自动播放
    const stop = () => {
      if (timer.value) clearInterval(timer.value)
    }
    // 鼠标移出轮播图,开启定时器
    const start = () => {
      if (props.findBannerList.length > 1 && props.autoplay) {
        autoplayFn()
      }
    }
    // 点击轮播图上的左右按钮,切换轮播图,通过传递进来的参数,决定轮播图往左往右
    const clickFn = e => {
      index.value += e
      if (index.value >= props.findBannerList.length) {
        index.value = 0
      }
      if (index.value < 0) {
        index.value = props.findBannerList.length - 1
      }
    }
    // 点击指示器(轮播图底下的小点)切换轮播图
    const active = e => {
      index.value = e
    }
    // 组件销毁的时候情书定时器,避免性能损耗
    onUnmounted(() => {
      if (timer.value) clearInterval(timer.value)
    })
    return { index, stop, start, clickFn, active }
  }
}
</script>
<style scoped lang="less">
.my-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>

二、封装成插件

import MyCarousel from './my-carousel.vue'
export default {
  install(app) {
    app.component(MyCarousel.name, MyCarousel)
  }
}

三、在入口文件 main.js 中全局注册

import { createApp } from 'vue'
import App from './App.vue'
import MyUI from './components/library'

// 插件的使用,在main.js使用app.use(插件)
createApp(App).use(MyUI).mount('#app')

四、在项目中使用组件

准备home-banner组件,使用my-carousel组件,然后在项目中使用轮播的地方引入 home-banner 组件, 下面的参数可以在 home-banner 组件中设置

findBannerList 参数作为,后台请求数据给到组件内部

autoplay 参数是否开启轮播,默认 true 开启轮播

duration 参数轮播停留时间间隔以 秒 为单位

<template>
  <div class="home-banner">
    <MyCarousel :findBannerList="findBannerList" :autoplay="true" :duration="3" />
  </div>
</template>

总结

按照思路步骤,一步步实现即可。

1.基本组件拆分和布局
2.自动轮播
3.悬停控制启动和停止
4.手动控制切换
5.销毁定时器
6.抽取相关的参数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Vue3.0手写轮播图效果

    本文实例为大家分享了Vue3.0手写轮播图效果的具体代码,供大家参考,具体内容如下 让我们开始把 html结构 <template> <div class="xtx-carousel" @mouseleave="enterFn" @mouseenter="leaveFn"> <ul class="carousel-body"> <li class="carousel-item

  • vue+rem自定义轮播图效果

    使用vue+rem自定义轮播图的实现,供大家参考,具体内容如下 单位使用rem进行页面布局,在动态计算轮播图整体宽度时,需要把px转换成rem,挺麻烦的. 效果如下:如果当前图片不是第一张和最后一张,刚好可以看到当前图片上一张和下一张的一部分. 具体代码如下 <template> <div class="constructionUp"> <div class="pub-hd"> <h2>施工升级包</h2>

  • uniapp vue与nvue轮播图 轮播图组件

    vue部分如下: <template> <view class=""> <!-- 轮播图组件 --> <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular=""> <block v-for="(it

  • vue3封装轮播图组件的方法

    目的 封装轮播图组件,直接使用,具体内容如下 大致步骤 准备my-carousel组件基础布局,全局注册 准备home-banner组件,使用my-carousel组件,再首页注册使用. 深度作用选择器覆盖my-carousel组件的默认样式 在home-banner组件获取轮播图数据,传递给my-carousel组件 在my-carousel组件完成渲染 自动播放,暴露自动轮播属性,设置了就自动轮播 如果有自动播放,鼠标进入离开,暂停,开启 指示器切换,上一张,下一张 销毁组件,清理定时器 落

  • Vue使用Swiper封装轮播图组件的方法详解

    目录 Swiper 为什么要封装组件 开始封装 1.下载安装Swiper 2.引入css样式文件 3.引入js文件 4.把官网使用方法中的HTML结构复制粘贴过来 5.初始化Swiper 自定义效果 完整代码 效果展示 Swiper Swiper是一个很常用的用于实现各种滑动效果的插件,PC端和移动端都能很好的适配. 官网地址:www.swiper.com.cn/ 目前最新版本是Swiper7,但众所周知最新版本通常不稳定,所以这里使用Swiper6来封装. Swiper各版本区别: 为什么要封

  • vue3.0封装轮播图组件的步骤

    接着上一篇文章,熟悉vue3.0的基本用法,和使用一段时间以后,开始准备开发适用于vue3.0使用的pc端的组件库.会陆续跟新一些组件库的写法和注意事项,有兴趣的同学可以多多关注哦,不多bb,开始. 开发一个轮播图组件,适用pc端,(暂无考虑app), 使用于vue3.0 + TS 大致的实现效果是这样: 图片自由轮播,对应圆点图片跳转,左右指示器跳转等.暴露以下options配置: 以上是主要的options,下面展开来说一下具体如何封装. 一:封装思想 在vue3.0和vue2.0中封装组件

  • vue3+Pinia+TypeScript 实现封装轮播图组件

    目录 为什么封装? 静态结构 后面再进行更改 请求数据都存放在pinia里面 类型检测 页面级组件 全局组件 为什么封装? 迎合es6模块化开发思想 注册为全局组件,可以更好地复用,需要用到的地方,直接使用标签即可 静态结构 后面再进行更改 <script lang="ts" setup name="XtxCarousel"> defineProps() </script> <template> <div class=&qu

  • Vue的轮播图组件实现方法

    今天在上慕课老师fishenal的vue实战课程的时候,有一个轮播图组件实现,在跟着做的时候,自己也踩了一些坑.此外,在原课程案例的基础上,我加入了不同方向的滑动功能. 本文章采用Vue结合Css3来实现轮播图. 首先要了解的是Vue的动画原理.在vue中,如果我们要给元素设置动画效果,则需要使用一个<transition name="targetClassName"></transition>将相应的元素包裹住,如下: <transition name=

  • UGUI轮播图组件实现方法详解

    本文实例为大家分享了UGUI轮播图组件实现的具体代码,供大家参考,具体内容如下 要用到,于是就自已做了一个,自认为封装上还是OK的,开发于unity5.1.2. 支持自动轮播.手势切换.代码调用切换,支持水平和竖直两个方向以及正负方向轮播,轮播索引改变有回调可以用,也可以获取到当前处于正中的子元素. 要注意的是,向轮播列表中加入新元素不能直接setparent,要调用该组件的AddChild方法 下面是鄙人的代码: /// 主要关注属性.事件及函数: /// public int Current

  • 微信小程序swiper轮播图组件使用方法详解

    本文实例为大家分享了微信小程序swiper轮播图组件的使用,供大家参考,具体内容如下 在components中新建文件夹swiper components/swiper/swiper.wxml <!--components/swiper/swiper.wxml--> <view class="container">     <swiper class="swiper-box" bind:change="swiperChange

  • vue.js轮播图组件使用方法详解

    之前用jQuery写过轮播组件,用的jquery动画实现的图片滑动效果.这个组件的滑动特效是原生js搭配vue的数据绑定实现的,不依赖其他库,虽然可以再vue.js中引入swiper,但是引入类库的最大的缺点就是冗余代码太多,所以还是自己写一个比较好,简单扼要.(ps:组件的宽高设置还有有点小bug,子组件中需要改为用js动态修改container的宽高,另外可能还有其他地方有不合理之处,欢迎各位批评指正) github地址:git@github.com:cainiao222/vueslider

  • android轮播图组件的制作方法

    本文实例为大家分享了android轮播图组件的制作方法,供大家参考,具体内容如下 BannerLayout package com.coral3.common_module.components; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.LayoutInfla

  • Vue中使用better-scroll实现轮播图组件

    better-scroll 是什么 better-scroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件.它的核心是借鉴的 iscroll 的实现,它的 API 设计基本兼容 iscroll,在 iscroll 的基础上又扩展了一些 feature 以及做了一些性能优化. better-scroll 是基于原生 JS 实现的,不依赖任何框架.它编译后的代码大小是 63kb,压缩后是 35kb,gzip 后仅有 9kb,是一款非常轻量的 JS lib. 今天我们利用它实现一个横向

随机推荐