133 lines
2.7 KiB
Vue
133 lines
2.7 KiB
Vue
<template>
|
|
<view>
|
|
<view class="maoScroll-main" :style="`height:${(lineHeight + props.mt) * showLine}rpx;`">
|
|
<view :style="`margin-top:-${marginTop}rpx;`">
|
|
<view
|
|
v-for="(item, index) in showdata"
|
|
:key="`maoScroll${index}`"
|
|
:style="`height:${lineHeight}rpx;margin-top:${props.mt}rpx;`"
|
|
|
|
>
|
|
<slot :line="item" />
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, watch, onMounted, onUnmounted, defineProps } from 'vue';
|
|
|
|
const props = defineProps({
|
|
data: {
|
|
type: Array,
|
|
default: () => [],
|
|
},
|
|
showNum: {
|
|
type: Number,
|
|
default: 3,
|
|
},
|
|
lineHeight: {
|
|
type: Number,
|
|
default: 60,
|
|
},
|
|
animationScroll: {
|
|
type: Number,
|
|
default: 500,
|
|
},
|
|
animation: {
|
|
type: Number,
|
|
default: 2000,
|
|
},
|
|
mt: {
|
|
type: Number,
|
|
default:16
|
|
}
|
|
});
|
|
const showdata = ref([]);
|
|
const marginTop = ref(0);
|
|
const showLine = ref(0);
|
|
let animationInterval = null;
|
|
let stepInterval = null;
|
|
|
|
// 初始化函数
|
|
const init = () => {
|
|
// 清除之前的定时器,避免内存泄漏
|
|
if (animationInterval) {
|
|
clearInterval(animationInterval);
|
|
}
|
|
if (stepInterval) {
|
|
clearInterval(stepInterval);
|
|
}
|
|
|
|
showLine.value = props.showNum < props.data.length ? props.showNum : props.data.length;
|
|
showdata.value = [];
|
|
|
|
// 填充数据
|
|
for (let i = 0; i < props.data.length; i++) {
|
|
showdata.value.push(props.data[i]);
|
|
}
|
|
for (let i = 0; i < showLine.value; i++) {
|
|
showdata.value.push(props.data[i]);
|
|
}
|
|
|
|
// 启动定时器
|
|
animationInterval = setInterval(animationFunc, props.animation);
|
|
};
|
|
|
|
// 动画函数
|
|
const animationFunc = () => {
|
|
if (marginTop.value >= props.data.length * props.lineHeight) {
|
|
marginTop.value = 0;
|
|
}
|
|
|
|
const stepTime = props.animationScroll / props.lineHeight;
|
|
let step = 0;
|
|
|
|
// 清除之前可能存在的步进定时器
|
|
if (stepInterval) {
|
|
clearInterval(stepInterval);
|
|
}
|
|
|
|
stepInterval = setInterval(() => {
|
|
marginTop.value++;
|
|
step++;
|
|
if (step >= props.lineHeight) {
|
|
clearInterval(stepInterval);
|
|
stepInterval = null;
|
|
}
|
|
}, stepTime);
|
|
};
|
|
|
|
// 监听数据变化
|
|
watch(
|
|
() => props.data,
|
|
() => {
|
|
init();
|
|
},
|
|
{ deep: true },
|
|
);
|
|
|
|
// 组件挂载时初始化
|
|
onMounted(() => {
|
|
init();
|
|
});
|
|
|
|
// 组件卸载时清理定时器
|
|
onUnmounted(() => {
|
|
if (animationInterval) {
|
|
clearInterval(animationInterval);
|
|
}
|
|
if (stepInterval) {
|
|
clearInterval(stepInterval);
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<style>
|
|
.maoScroll-main {
|
|
width: 100%;
|
|
overflow: hidden;
|
|
}
|
|
</style>
|