wepy写小程序之文字水平滚动

近期有一个显示文字水平滚动的需求,思考了半天写了几行小程序代码,一言不合就上代码:

wxml

<view class="rollCon">

    <view class="box">

        <view class="text"  style="left:{{offsetLeft}}px"> {{text}} </view>

    </view>

</view>

wxss

.rollCon{position: fixed;bottom: 0;left: 0;width: 100%;height: 60rpx;z-index: 100;background: #FDE8C7;font-size: 20rpx;line-height: 60rpx;}

.box {width: 100%;position: relative;}

.text {white-space: nowrap;position: absolute;top: 0;}//注意一定要用nowrap。。。

js

data={text:'共0张 价税合计:12121 金额:2323233 税额:232423',

pace: 1,//滚动速度

start: 0,//初始滚动距离

interval:20,//时间间隔

size:14,

length:0,

windowWidth:0,}

//文字走马灯

run(){

let that = this;

let interval = setInterval(function () {if (-that.start < that.length) {

that.start = that.start - that.pace;

that.$apply();

} else {

clearInterval(interval);

that.start = that.windowWidth;

that.run();

that.$apply();}

}, that.interval);}

onshow(){

//文字滚动

let length = (this.text.length-20) * this.size;//文字长度

let windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度

this.length = length;this.windowWidth = windowWidth;

this.$apply()

this.run();}

定时器每次触发时,调用this.$apply()来更新data数据 ,要不每次只执行一次,数据 得不到改变!!!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 晚上跟因为照顾宝宝甚久不见的好友吃饭,她絮絮叨叨跟我说了很多结婚生子之后的烦恼,最后告诫我:千万不要太早结婚!千万...
    方圆杂谈阅读 403评论 0 1
  • 我第三次写种花的事是因为我自己觉得有意思,又实在认为能学到东西。 我一周前在花盆里撒的种子现在已经隐隐地冒出芽来,...
    念念s阅读 311评论 0 0
  • 自从辞职后呆在家后我又重新加入了瑜伽班,一周上三次课一次课有俩小时都是在下午,性格活泼好动的我竟然能在这样的...
    小漫画_阅读 338评论 1 4
  • 本周过多的时间都用在关注区块链资产的价格浮动上了,虽没有操作,只是傻呵呵地看。真是“智商随着价格浮动而波动啊”.....
    Mimosa1228阅读 324评论 0 1