(component): [carousel] 新增 pauseOnHover 属性

This commit is contained in:
sight 2022-08-29 02:19:05 +08:00
parent 691641d655
commit eee74b05f0

View File

@ -32,6 +32,7 @@ const props = withDefaults(
arrow?: string; arrow?: string;
interval?: number; interval?: number;
indicator?: string; indicator?: string;
pauseOnHover?: boolean;
}>(), }>(),
{ {
width: "100%", width: "100%",
@ -41,6 +42,7 @@ const props = withDefaults(
arrow: "hover", arrow: "hover",
interval: 3000, interval: 3000,
indicator: "inside", indicator: "inside",
pauseOnHover: true,
} }
); );
@ -133,10 +135,33 @@ const autoplay = () => {
} }
}; };
let intervalTimer = 0;
const cleanIntervalTimer = () => {
if (intervalTimer) {
window.clearInterval(intervalTimer);
intervalTimer = 0;
}
};
const handleMouseEnter = () => {
if (props.autoplay && props.pauseOnHover) {
cleanIntervalTimer();
}
};
const handleMouseLeave = () => {
if (props.autoplay && props.pauseOnHover) {
intervalTimer = window.setInterval(autoplay, props.interval);
}
};
watch( watch(
() => props.autoplay, () => props.autoplay,
() => { () => {
if (props.autoplay) setInterval(autoplay, props.interval); if (props.autoplay) {
intervalTimer = window.setInterval(autoplay, props.interval);
}
}, },
{ immediate: true } { immediate: true }
); );
@ -149,6 +174,8 @@ watch(
:lay-indicator="indicator" :lay-indicator="indicator"
:lay-arrow="arrow" :lay-arrow="arrow"
:style="{ width: width, height: height }" :style="{ width: width, height: height }"
@mouseenter="handleMouseEnter"
@mouseleave="handleMouseLeave"
> >
<div carousel-item> <div carousel-item>
<slot></slot> <slot></slot>