美文网首页
文字超出滚动 uni-app组件

文字超出滚动 uni-app组件

作者: 吖蛋黄 | 来源:发表于2020-04-24 17:16 被阅读0次

一、预览图

文字超出滚动.gif

二、实现的点

1.文字超出父元素宽度,自动滚动
2.文字不超出父元素宽度,不滚动
3.自动计算滚动时间,控制滚动速度
4.兼容H5、小程序

三、实现代码

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() {
                // 等待节点挂载后再执行,热门线路tip滚动实现
                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: '很长很长很长很长很长很长很长很长很长很长很长很长的文字'
      }
   }
}

相关文章

  • 文字超出滚动 uni-app组件

    一、预览图 二、实现的点 1.文字超出父元素宽度,自动滚动2.文字不超出父元素宽度,不滚动3.自动计算滚动时间,控...

  • css中overflow的使用

    1)hidden :将超出部分隐藏 overflow: hidden; 2)auto:文字超出出现滚动条,文字没有...

  • 总结1

    键盘弹起 显示超出时(溢出时)加SingleChildScrollView 单独滚动组件SingleChild...

  • 容器-滚动控制器

    就是超出父级元素宽度出现滚动条 只用横向或者只用竖向的组件

  • cocos2dx 3.x 公告栏 通知栏 滚动字幕 区域弹幕 区

    本文介绍游戏中常见的滚动播放的公告栏实现 要点 文字内容横向或者纵向滚动 文字只在区域内显示,超出区域部分不显示 ...

  • Element-UI滚动条组件封装

    当内容超出固定区域后,会出现滚动条,浏览器自带的滚动条样式比较丑,所以可以使用el-scrollbar的组件进行优...

  • 中奖轮播公告 文字轮播

    中奖轮播公告,在SDCycleScrollView的基础上增加了一个属性来设置是否允许文字超出宽度后向右滚动,超出...

  • sass 中常用class 总结

    清除浮动 滚动条样式(隐藏) 强制不换行超出文字省略号 多行情况下第line行超出文字省略号 $px为需要转换的rem

  • [每天进步一点点~] uni-app scroll-view组件

    虽然uni-app官方文档里,scroll-view组件的滚动条是默认不显示的,但是有时候运行到移动端或者不同机型...

  • vue 滚动和过度动画

    1. 组件内滚动 组件内滚动到指定锚点 scrollIntoView在组件中 组件内滚动到指定位置初始化的化滚动,...

网友评论

      本文标题:文字超出滚动 uni-app组件

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