This commit is contained in:
HongRun Liu
2026-05-26 15:52:30 +08:00
commit b0744c8ab6
849 changed files with 130587 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
<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>
+83
View File
@@ -0,0 +1,83 @@
<template>
<view>
<view class="maoScroll-main" :style="'height:'+(lineHeight*showLine)+'rpx;'">
<view :style="'margin-top:-'+marginTop+'rpx;'">
<view v-for="(item,index) in showdata" :key="'maoScroll'+index" :style="'height:'+lineHeight+'rpx;'">
<slot :line="item" />
</view>
</view>
</view>
</view>
</template>
<script>
export default {
name: 'maoScroll',
data() {
return {
showdata: [],
marginTop: 0,
showLine: 0,
}
},
props:{
data: {
type: Array,
default: []
},
showNum: {
type: Number,
default: 3,
},
lineHeight: {
type: Number,
default: 60,
},
animationScroll: {
type: Number,
default: 500,
},
animation: {
type: Number,
default: 2000,
}
},
methods: {
init: function(){
this.showLine = this.showNum < this.data.length ? this.showNum : this.data.length;
for(let i = 0; i < this.data.length; i++){
this.showdata.push(this.data[i]);
}
for(let i = 0; i < this.showLine; i++){
this.showdata.push(this.data[i]);
}
setInterval(this.animationFunc, this.animation);
},
animationFunc: function(){
if(this.marginTop >= this.data.length*this.lineHeight){
this.marginTop = 0;
}
let stepTime = this.animationScroll/this.lineHeight;
var step = 0;
let self = this;
var index = setInterval(function(){
self.marginTop = self.marginTop + 1;
step++;
if (step >= self.lineHeight) {
clearInterval(index);
}
}, stepTime);
}
},
watch: {
data(outdata, newdata) {
this.init();
}
}
}
</script>
<style>
.maoScroll-main{width: 100%;overflow: hidden;}
</style>