自己封装组件时,先把静态的页面写好,然后再去把静态的变为动态数据,一般简单的数据的改动用props,如果是复杂的改动,则需要用插槽,多个的话需要具名插槽,如果有多个插槽,有一个没给名字,则默认名字是default
<template>
<div class="box" style="height:500px">
<div class="xtx-slider " @mouseenter="mouseenter" @mouseleave="mouseleave">
<!-- 图片列表 -->
<ul class="slider-body">
<!--
fade: 当fade类名存在 当前图片就显示 不存在就不显示
-->
<li
class="slider-item "
v-for="(item, i) in sliders"
:key="i"
:class="{ fade: curIndex === i }"
>
<img :src="item.imgUrl" alt="" />
<!-- 左右箭头 这里用的字体图标显示箭头,如果没有引入字体图标可以自己写在这里插入代码片 -->
<i class="slider-btn prev iconfont icon-angle-left" @click="leftClick"></i>
<i class="slider-btn next iconfont icon-angle-right" @click="rigthClick"></i>
</li>
</ul>
<!-- 圆圈切换按钮 -->
<div class="slider-indicator ">
<span :class="{active:curIndex===index}"
v-for="(item,index) in sliders"
:key="item.id"
@click="handerClick(index)"></span>
</div>
</div>
</div>
</template>
<script>
/**
* 目标:点击圆圈按钮 实现对应图片的切换
* 思路:
* 1. 图片和圆圈按钮数量是一样的 下标值是对应的
* 2. 记录一下当前点击的是哪一项
* 3. 需要根据记录下来的下标值 去配合:class 控制fade这个类名是否应该显示
*/
// 自动切换
// 思路 下标自增
// 进入停止图片
/** 鼠标进入事件,移除时间
开启定时器,关闭定时器 优化
*/
// props
/**
* 依赖数据
* 自定义时间
* 是否开启自动播放
*/
import { ref, onUnmounted, onMounted } from 'vue'
export default {
name: 'XtxSlider',
props: {
sliders: { // 数据列表
type: Array,
required: true,
default: () => {
return []
}
},
times: { // 定时器默认间隔时间
type: Number,
default: 1500
},
isAuto: { // 是否开启定时器,默认开启
type: Boolean,
default: true
}
},
setup (props) {
const curIndex = ref(0)
// const sliders = ref([])
// 点击圆圈
function handerClick (index) {
curIndex.value = index
}
let timer = null
// 封装一个开启定时器
function startTimer () {
// 在这里判断是否开启自动播放 难点
if (!props.isAuto) {
return
}
timer = setInterval(() => {
curIndex.value++
if (curIndex.value === props.sliders.length) {
curIndex.value = 0
}
}, props.times)
}
// 封装一个关闭定时器
function stopTime () {
clearInterval(timer)
}
// 鼠标进入关闭
function mouseenter () {
stopTime()
}
// 鼠标移除开启
function mouseleave () {
startTimer()
}
// 左箭头
function leftClick () {
curIndex.value++
if (curIndex.value === props.sliders.length) {
curIndex.value = 0
}
}
// 右箭头
function rigthClick () {
curIndex.value--
if (curIndex.value < 0) {
curIndex.value = props.sliders.length - 1
}
}
// 默认打开就启动
onMounted(() => {
startTimer()
})
// 清除定时器
onUnmounted(() => {
stopTime()
})
return {
curIndex,
// sliders,
handerClick,
mouseenter,
mouseleave,
leftClick,
rigthClick
}
}
}
</script>
<style scoped lang='less'>
.xtx-slider {
width: 100%;
height: 100%;
min-width: 300px;
min-height: 150px;
position: relative;
.slider {
&-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;
cursor: pointer;
&.prev {
left:270px;
}
&.next {
right: 20px;
}
}
}
&:hover {
.slider-btn {
// 关键代码
opacity: 1;
}
}
}
</style>