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

 更新时间:2022年07月25日 09:41:57   作者:​ xue_zhiyuan​  
这篇文章主要介绍了vue3+Pinia+TypeScript 实现封装轮播图组件,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下

为什么封装?

  • 迎合es6模块化开发思想
  • 注册为全局组件,可以更好地复用,需要用到的地方,直接使用标签即可

静态结构 后面再进行更改

<script lang="ts" setup name="XtxCarousel">
defineProps()
</script>
<template>
  <div class="xtx-carousel">
    <ul class="carousel-body">
      <li class="carousel-item fade">
        <RouterLink to="/">
          <img
            src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
            alt=""
          />
        </RouterLink>
      </li>
      <li class="carousel-item">
        <RouterLink to="/">
          <img
            src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
            alt=""
          />
        </RouterLink>
      </li>
      <li class="carousel-item">
        <RouterLink to="/">
          <img
            src="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"
            alt=""
          />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="carousel-btn prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="carousel-btn next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span class="active"></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-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>

请求数据都存放在pinia里面

import { defineStore } from 'pinia'
import request from '@/utils/request'
import { BannerItem, IApiRes } from '@/types/data'

export default defineStore('home', {
  persist: {
    enabled: true
  },
  
  state() {
    return {
      bannerList: [] as BannerItem[]
    }
  },
  actions: {
    // 拿轮播图数据
    async getBannerList() {
      const res = await request.get<IApiRes<BannerItem[]>>('/home/banner')
      console.log('拿轮播图数据', res)
      this.bannerList = res.data.result
    }
  }
})

类型检测

// 所有的接口的通用类型
export interface IApiRes<T> {
  msg: string,
  result: T
}

// 轮播图数据中的项
export type BannerItem = {
  id: string;
  imgUrl: string;
  hrefUrl: string;
  type: string;
}

页面级组件

<script lang="ts" setup>
import useStore from '@/store';
const { home } = useStore()
// 发请求
home.getBannerList()

</script>
<template>
  <div class="home-banner">
    <!-- 轮播图子组件 -->
    <XtxCarousel :slides="home.bannerList" :autoPlay="true" :duration="1000"/>
  </div>
</template>

<style scoped lang="less">
.home-banner {
  width: 1240px;
  height: 450px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 98;
  background-color: #ccc;
}
/deep/ .xtx-carousel .carousel-btn.prev {
  left: 270px !important;
}
/deep/ .xtx-carousel .carousel-indicator {
  padding-left: 250px;
}
</style>

全局组件

<script lang="ts" setup name="XtxCarousel">
import { BannerItem } from '@/types/data'
import { onBeforeUnmount, onMounted, ref } from 'vue';
// 定义props
const { slides, autoPlay, duration } = defineProps<{
  slides: BannerItem[], 
  autoPlay?: boolean, // 是否开启自动播放
  duration?: number // 自动播放的间隔时间
}>()

// 当前哪个图片选中 高亮
const active = ref(1)

// 点击右侧箭头++
const hNext = () => {
  active.value++
  if(active.value === slides.length){
    active.value = 0
  }
}

// 点击左侧箭头--
const hPrev = () => {
  active.value--
  if(active.value < 0){
    active.value = slides.length - 1
  }
}

// 如果开启了自动播放,则每隔duration去播放下一张: hNext()
onMounted(() => {
  start()
})

onBeforeUnmount(() => {
  stop()
})

let timer = -1

// 鼠标离开要继续播放
const start = () => {
  if(autoPlay) {
    timer = window.setInterval(() => {
      hNext()
    }, duration)
  }
}

// 鼠标hover要暂停播放
const stop = () => {
  clearInterval(timer)
}
</script>

<template>
  <div class="xtx-carousel" @mouseleave="start()" @mouseenter="stop()">
    <ul class="carousel-body">
      <li class="carousel-item" 
      :class="{ fade: idx === active}"
      v-for="(it, idx) in slides" :key="it.id">
        <RouterLink to="/">
          <img :src="it.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a @click="hPrev" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="hNext" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span
      v-for="(it, idx) in slides"
      :class="{ active: active===idx}"
      @mouseenter="active = idx"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-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>

到此这篇关于vue3+Pinia+TypeScript 实现封装轮播图组件的文章就介绍到这了,更多相关vue3 封装轮播图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • json数据处理技巧(字段带空格、增加字段、排序等等)

    json数据处理技巧(字段带空格、增加字段、排序等等)

    json数据处理技巧例如:正常取值、字段带空格、赋值、增加字段、排序、拷贝、数组添加和删除等,详细请参考本文或许对你有所帮助
    2013-06-06
  • mint-ui的search组件在键盘显示搜索按钮的实现方法

    mint-ui的search组件在键盘显示搜索按钮的实现方法

    这篇文章主要介绍了mint-ui的search组件在键盘显示搜索按钮的实现方法,需要的朋友可以参考下
    2017-10-10
  • 微信小程序实现天气预报功能(附源码)

    微信小程序实现天气预报功能(附源码)

    这篇文章主要介绍了微信小程序实现天气预报功能(附源码),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • javascript模拟命名空间

    javascript模拟命名空间

    JavaScript 没有任何特定语言功能来支持命名空间,但很容易使用对象来模拟命名空间。今天我们就来探讨下这个问题,希望大家能够喜欢。
    2015-04-04
  • 11款基于Javascript的文件管理器

    11款基于Javascript的文件管理器

    11款基于JavaScript的文件管理器,大多数免费开源,功能并不逊色于那些专业的文件管理程序。
    2009-10-10
  • js无提示关闭浏览器窗口的两种方法分析

    js无提示关闭浏览器窗口的两种方法分析

    这篇文章主要介绍了js无提示关闭浏览器窗口的两种方法分析,需要的朋友可以参考下
    2016-11-11
  • Javascript异步执行不按顺序解决方案

    Javascript异步执行不按顺序解决方案

    这篇文章主要介绍了Javascript异步执行不按顺序解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 利用Query+bootstrap和js两种方式实现日期选择器

    利用Query+bootstrap和js两种方式实现日期选择器

    日期选择器在我们平时开发的时候经常要用到,下面这篇文章主要给大家介绍了利用Query+bootstrap和js这两种方式实现日期选择器的方法,文中两种方法都给出了详细的示例代码,有需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • JS模拟实现京东快递单号查询

    JS模拟实现京东快递单号查询

    这篇文章主要为大家详细介绍了JS模拟实现京东快递单号查询,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • JS使用Promise控制请求并发数

    JS使用Promise控制请求并发数

    现在面试过程当中 ,手写题必然是少不了的,其中碰到比较多的无非就是当属 请求并发控制了,所以本文为大家整理了JS使用Promise控制请求并发数的示例代码,希望对大家有所帮助
    2023-05-05

最新评论