美文网首页前端开发日常
vue3数字上下滚动显示动效

vue3数字上下滚动显示动效

作者: jaqqq | 来源:发表于2023-07-18 11:26 被阅读0次

实现动效如下:

2023-07-19 11.10.26.gif 2023-07-19 11.10.53.gif
number-ad.jpg

代码具体如下:

  • vue3+ts+setup 封装组件

第一步封装组件 NumberAnimation.vue

  <template>
    <div class="number-digital">
        <div class="box-item">
            <li :class="{ 'number-item': !isNaN(item), 'mark-item': isNaN(item) }"
                :style="{ marginRight: needDivideMgright(index) }" v-for="(item, index) in orderNum" :key="index">
                <span v-if="!isNaN(item)">
                    <i :id="`numberItem${index}`">0123456789</I>
                </span>
                <div v-if="needDivideDot(index)" class="num-dot">,</div>
            </li>
        </div>
    </div>
</template>
   
<script lang="ts" setup>
const props = defineProps<{
    numm: number | string
    numLen: number
    noneDivide?: boolean // 默认需要超过7位数字以上以,分隔
}>()
function needDivideDot(index: number): boolean {
    if (props.noneDivide) { //不需要分隔符
        return false
    }
    if (props.numLen <= 7) {
        return false
    }
    return index === props.numLen - 8 || index === props.numLen - 5
}
function needDivideMgright(index: number): string {
    if (props.noneDivide) { //不需要分隔符
        return '2px'
    }
    if (props.numLen <= 7) {
        return '2px'
    }
    return (index === props.numLen - 7 || index === props.numLen - 4) ? '14px' : '2px'
}

const orderNum = ref<Array<number>>([]);
// 处理数字
// numm: 传入的数字  numLen: 默认显示几位数
const toOrderNum = (num: string | number) => {
    num = num.toString();
    if (num.length < props.numLen) {
        num = '0' + num // 如未满定义的位数,添加"0"补位
        toOrderNum(num) // 递归添加"0"补位
    } else if (num.length === props.numLen) {
        orderNum.value = num.split('') as unknown as number[] // 将其便变成数据,渲染至滚动数组
    }

}
watch(() => props.numm, (newVal: any) => {
    toOrderNum(newVal)
    setNumberTransform()
})
function setNumberTransform() {
    for (let index = 0; index < orderNum.value.length; index++) {
        const ele: HTMLDivElement = document.getElementById(`numberItem${index}`) as HTMLDivElement
        if (ele) {
            ele.style.transform = `translate(-50%, -${orderNum.value[index] as number * 10}%)`
        }
    }
}
toOrderNum(props.numm)
onMounted(() => {
    setTimeout(() => {
        setNumberTransform()
    }, 200)
})

</script>
<style lang='scss' scoped>
.number-digital {
    margin-right: 8px;

    .box-item {
        position: relative;
        font-size: 54px;
        line-height: 41px;
        text-align: center;
        list-style: none;
        color: #2D7CFF;
        writing-mode: vertical-lr;
        text-orientation: upright;
        /*文字禁止编辑*/
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        -khtml-user-select: none;
        user-select: none;
    }

    /*滚动数字设置*/
    .number-item {
        width: 50px;
        height: 56px;
        color: #fff;
        font-size: 40px;
        font-family: MiSans, MiSans-Demibold;
        font-weight: 800;
        list-style: none;
        margin-right: 3px;
        background: rgba(3, 21, 42, 0.50);
        border: 1px solid #4d9ef8;
        box-shadow: 0px 0px 3px 0px #01072E;

        &>span {
            position: relative;
            display: inline-block;
            margin-right: 10px;
            width: 100%;
            height: 100%;
            writing-mode: vertical-rl;
            text-orientation: upright;
            overflow: hidden;

            &>I {
                font-style: normal;
                position: absolute;
                top: 8px;
                left: 50%;
                transform: translate(-50%, 0);
                transition: transform 1s ease-in-out;
                letter-spacing: 10px;
            }
        }
    }

    .number-item:last-child {
        margin-right: 0;
    }

    .num-dot {
        display: inline-block;
        margin-left: 8px;
        height: 53px;
        font-size: 47px;
        font-family: MiSans, MiSans-Demibold;
        font-weight: normal;
        text-align: CENTER;
        color: #ffffff;
        line-height: 47px;
    }

}
</style>
  

第二步 使用 NumberAnimation.vue组件

 <!-- 逗号分隔 (需要import引入组件) -->
            <NumberAnimation :numm="rand" :numLen="9"></NumberAnimation>
 <!-- 不进行逗号分隔 (需要import引入组件) -->
            <NumberAnimation :numm="rand" :numLen="9" :noneDivide="true"></NumberAnimation>

相关文章

  • DIV CSS 上下左右滑动

    div显示上下左右滚动条 这里是你要显示的内容 div显示上下滚动条的css代码 这里是你要显示的内容 div...

  • 数字变化动效

    在开发过程中经常会遇到数字从0不断变动的动画效果,在接到这样的需求后,我第一时间想到了通过计时器的方式对数字进行不...

  • 多条跑马灯

    实现 1.一条上下不滚动,多条上下来回滚动。2.单条没有超出显示区域不左右滚动,超出左右来回滚动。 下载

  • 书签收藏

    UI 动效 手机滚动组件 http://www.woshipm.com/pd/88470.html http://...

  • UITableView视差滚动效果

    OCiOS动效设计:UITableView 实现滚动视差效果 - 李鴻耀 - 博客频道 - CSDN...

  • 动效解决方案ChoreographerJS

    一、choreographerJS 一个实现滚动实现复杂动效的库 官网 https://christinecha....

  • Fade数字切换动效

    欢迎同样喜爱动效的你加入iOS动效特攻队–>QQ群:547897182 iOS动效特攻队–>熊熊:64807025...

  • 早起的魅力

    今日清晨,我像往常一样风风火火的出门,看着电梯数字显示牌上上下下滚动的数字,我的内心既焦灼又侥幸淡定:我出来这么晚...

  • 关于动态加载,滚动条导致抖动解决方案

    应为刚开始高度没有滚动条,后来请求数据回来高度增加导致滚动条显示,加上下面一行,不管高度够不够,都显示滚动条。

  • 滚动显示数字文本封装

    gibhub地址:ScrollNumber 效果图 功能及什么使用 一、实现了数字滚动增长动画(主要包含三种类型数...

网友评论

    本文标题:vue3数字上下滚动显示动效

    本文链接:https://www.haomeiwen.com/subject/uqynudtx.html