vue3+vite+ts 实现大屏数字滚动效果

样式用的是windicss

效果

代码

<template>
	<div class="w-full flex justify-center items-center">
		<div
			v-for="(item, index) in numList"
			:key="index"
			class="num-item w-32px h-42px text-26px text-center border border-hex-8fbbff rounded leading-42px"
		>
			<div v-for="(child, childIndex) in 10" :key="childIndex" :style="{ ...style(item) }">
				{{ childIndex }}
			</div>
		</div>
	</div>
</template>
<script setup lang="ts">
import { computed, watch } from 'vue'
const props = defineProps({
	number: {
		type: Number,
		default: 0
	},
	// 位数
	quantity: {
		type: Number,
		default: 5
	},
	// 是否从零开始变化
	delayed: {
		type: Boolean,
		default: true
	},
	// 过度时间
	time: {
		type: Number,
		default: 2000
	},
	// 过度动画速度
	timing: {
		type: String,
		default: 'ease'
	}
})
const num = ref(0)
const numArr = computed(() => {
	if (num.value) {
		return (num.value + '').split('')
	} else {
		return new Array(num.value.length).fill(0)
	}
})
const numList = computed(() => {
	let arr = []
	let len = numArr.value.length
	if (props.quantity && props.quantity > len) {
		arr = [...new Array(props.quantity - len).fill(0), ...numArr.value]
	} else {
		arr = numArr.value
	}
	return arr
})
watch(
	() => props.number,
	(newValue, oldValue) => {
		num.value = newValue
	}
)
const style = (e: any) => {
	return {
		transform: `translateY(-${e * 100}%)`,
		transition: props.time + 'ms',
		transitionTimingFunction: props.timing
	}
}
onMounted(() => {
	if (props.delayed) {
		setTimeout(() => {
			num.value = props.number
		}, 300)
	} else {
		num.value = props.number
	}
})
</script>
<style lang="scss" scoped>
.num-item {
	background-image: linear-gradient(180deg, #493fffad 0%, #194ccfb3 100%);
	overflow: hidden;
	font-family: Impact;
	text-shadow: 0 2px 4px #0000002e;
	margin-right: 12px;
	&:last-child {
		margin-right: 0;
	}
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值