当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现简单文字跑马灯

微信小程序实现简单文字跑马灯

2020年06月23日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了微信小程序实现文字跑马灯 的具体代码,供大家参考,具体内容如下

效果如图

wxml

<scroll-view class="container">
 <view class="scrolltxt">
 <view class="marquee_box">
 <view class="marquee_text" style="transform: translatex(-{{marqueedistance}}px)">
 <text>{{text}}</text>
 <text style="margin-right:{{marquee_margin}}px;"></text>
 <text style="margin-right:{{marquee_margin}}px;">{{text}}</text> 
 </view>
 </view>
 </view>
</scroll-view>

wxss

.container {display: flex;flex-direction: column;justify-content: space-between;box-sizing: border-box;}
.scrolltxt{padding:0 20rpx;background:#f8f8f8;}
.marquee_box {position:relative;color:#333;height:90rpx;display:block;overflow:hidden;} 
.marquee_text {white-space: nowrap;position:absolute;top:0;font-size:14px;height:90rpx;line-height:90rpx;}

js

 data: {
 //滚动字幕
 text: "温馨提示:为了保证其他购买者的利益,每个 账号针对同一商品只允许退款一次,请您谨慎操作。",
 marqueepace: 1,//滚动速度
 marqueedistance: 0,//初始滚动距离
 marquee_margin: 30,//间距
 size: 14,//字号
 time: '',// 定时器
 },

onhide: function () {
 //清空滚动字幕定时器,避免锁屏或页面隐藏后移动速度越来越快
 clearinterval(this.data.time);
 },

 onshow: function (e) {
 var that = this;
 var length = that.data.text.length * that.data.size;//文字长度
 var windowwidth = wx.getsysteminfosync().windowwidth;// 屏幕宽度
 //console.log(length,windowwidth);
 that.setdata({
 length: length,
 windowwidth: windowwidth
 });
 that.scrolltxt();// 第一个字消失后立即从右边出现
 },

 /**
 * 滚动字幕
 */
 scrolltxt: function () {
 var that = this;
 var length = that.data.length;//滚动文字的宽度
 var windowwidth = that.data.windowwidth;//屏幕宽度
 var time = this.data.time
 if (length > windowwidth) {
 time = setinterval(function () {
 var maxscrollwidth = length + that.data.marquee_margin;//滚动的最大宽度,文字宽度+间距,如果需要一行文字滚完后再显示第二行可以修改marquee_margin值等于windowwidth即可
 var crentleft = that.data.marqueedistance;
 if (crentleft < maxscrollwidth) {//判断是否滚动到最大宽度
  that.setdata({
  marqueedistance: crentleft + that.data.marqueepace
  })
 }
 else {
  that.setdata({
  marqueedistance: 0 // 直接重新滚动
  });
  clearinterval(time);
  that.scrolltxt();
 }
 }, 20);
 }
 else {
 that.setdata({ marquee_margin: "1000" });//只显示一条不滚动右边间距加大,防止重复显示
 }
 that.setdata({
 time:time
 })
 },

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网