文字超出滾動 uni-app組件

一、預(yù)覽圖

文字超出滾動.gif

二、實現(xiàn)的點

1.文字超出父元素寬度,自動滾動
2.文字不超出父元素寬度,不滾動
3.自動計算滾動時間,控制滾動速度
4.兼容H5、小程序

三、實現(xiàn)代碼

components/textScroll/index.vue 組件代碼如下:

<template>
    <div class="tip">
        <div class="inner" :class="{'move': scroll}" :style="styleName">
            <text class="tip-inder">{{text}} {{scroll ? text : '' }}</text>
        </div>
    </div>
</template>

<script>
    export default {
        props: {
            text: {
                type: String,
                defualt: ''
            },
        },
        data() {
            return {
                styleName: "animation-duration: 6s",
                scroll: false,
                tipWidth: 0
            };
        },
        watch: {
            text: {
                handler(val) {
                    this.textScroll()
                },
                immediate: false
            }
        },
        methods: {
            textScroll() {
                // 等待節(jié)點掛載后再執(zhí)行,熱門線路tip滾動實現(xiàn)
                this.$nextTick(() => {
                    let query = wx.createSelectorQuery().in(this)
                    query.select('.tip').boundingClientRect(data => {
                        this.tipWidth = data.width
                        console.log('tip', this.tipWidth)
                    }).exec();
                    query.select('.tip-inder').boundingClientRect(data => {
                        console.log('tip-inder', data.width)
                        if(data.width > this.tipWidth) {
                            let w = Number(data.width)
                            let time = Math.round(w / 40)
                            this.styleName = `animation-duration: ${time}s`
                            this.scroll = true
                        }
                    }).exec();
                })
            }
        }
    };
</script>

<style lang="less">
    .tip {
        width: 100%;
        background: #f6f9ff;
        color: #4d82ff;
        font-size: 28rpx;
        height: 80rpx;
        line-height: 80rpx;
        overflow: hidden;
        box-sizing: border-box;
        white-space: nowrap;
    }
    
    .tip .inner {
        overflow: hidden;
        display: inline-block;
    }

    .tip .inner .tip-inder {
        white-space: nowrap;
        padding-left: 30rpx;
    }

    .tip .inner.move {
        // animation: move 6s infinite linear;
        animation-name: scroll;
        animation-timing-function: linear;
        animation-iteration-count: infinite;
    }

    @keyframes scroll {
        0% {
            transform: translateX(0);
        }

        100% {
            transform: translateX(-50%);
        }
    }
</style>


四、使用教程

  • vue頁面使用
<template>
    <text-scroll :text="boardRemark"></text-scroll>
</template>

import textScroll from '@/components/textScroll/index.vue'
export default {
    components: { textscroll },
    data() {
        return {
                boardRemark: '很長很長很長很長很長很長很長很長很長很長很長很長的文字'
      }
   }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容