轮播图:有限的空间,循环展示多个内容
功能:
- 图片切换显示
 - 上一张,下一张按钮点击
 - 底部指示条高亮
 - 自动播放,鼠标移入开始,鼠标移出继续
 
一: 封装轮播图:
创建 src/components/xtx-carousel.vue
<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></ul><a href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a><a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a><div class="carousel-indicator"><span v-for="i in 5" :key="i"></span></div></div></template><script>export default {name: 'XtxCarousel'}</script><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,.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 XtxCarousel from './xtx-carousel.vue'const myPlugin = {install (app) {app.component(XtxCarousel.name, XtxCarousel)}}export default myPlugin ----> 然后在 main.js里面引入
三: 渲染结构
步骤:
- 定义获取广告图API函数
 - 在父组件获取轮播图数据,传递给xtx-carousel组件 父还传了一个duration()
 - 在xtx-carousel组件完成渲染 v-for渲染图片
 
注意:
  fade这个类是用来控制显示当前图片的; active激活指示条
需要一个索引数据,默认渲染第一张图和激活指示条中的第一个点
四: 逻辑封装
一: 自动播放:
- 暴露自动轮播属性,设置了就自动轮播
 - 暴露自动播放间隔时间
 - 如果有自动播放,鼠标进入离开,暂停,开启
 销毁组件时清理定时器
- 离开暂停: 如果有自动播放,鼠标进入离开,暂停,开启
 
- 销毁组件,清理定时器
 
二: 上下切换指示器
思路: 封装一个函数,处理上一张,下一张按钮的回调。

  
   
