Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of character Racing Lantern on Mini Program

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容主要讲解"小程序怎么实现文字跑马灯效果",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"小程序怎么实现文字跑马灯效果"吧!

wxml

1 显示完后再显示

{{text}}

2 出现白边后即显示

{{text}}

{{text}}

wxss

.example {

display: block;

width: 100%;

height: 100rpx;

}

.marquee_box {

width: 100%;

position: relative;

}

.marquee_text {

white-space: nowrap;

position: absolute;

top: 0;

}

js

// pages/home/marquee/marquee.js

Page({

data: {

text: '这是一条会滚动的文字滚来滚去的文字跑马灯,哈哈哈哈哈哈哈哈',

marqueePace: 1,//滚动速度

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

marqueeDistance2: 0,

marquee2copy_status: false,

marquee2_margin: 60,

size: 14,

orientation: 'left',//滚动方向

interval: 20 // 时间间隔

},

onShow: function () {

// 页面显示

var vm = this;

var length = vm.data.text.length * vm.data.size;//文字长度

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

vm.setData({

length: length,

windowWidth: windowWidth,

marquee2_margin: length < windowWidth ? windowWidth - length : vm.data.marquee2_margin//当文字长度小于屏幕长度时,需要增加补白

});

vm.run1();// 水平一行字滚动完了再按照原来的方向滚动

vm.run2();// 第一个字消失后立即从右边出现

},

run1: function () {

var vm = this;

var interval = setInterval(function () {

if (-vm.data.marqueeDistance < vm.data.length) {

vm.setData({

marqueeDistance: vm.data.marqueeDistance - vm.data.marqueePace,

});

} else {

clearInterval(interval);

vm.setData({

marqueeDistance: vm.data.windowWidth

});

vm.run1();

}

}, vm.data.interval);

},

run2: function () {

var vm = this;

var interval = setInterval(function () {

if (-vm.data.marqueeDistance2 < vm.data.length) {

// 如果文字滚动到出现marquee2_margin=30px的白边,就接着显示

vm.setData({

marqueeDistance2: vm.data.marqueeDistance2 - vm.data.marqueePace,

marquee2copy_status: vm.data.length + vm.data.marqueeDistance2 = vm.data.marquee2_margin) { // 当第二条文字滚动到最左边时

vm.setData({

marqueeDistance2: vm.data.marquee2_margin // 直接重新滚动

});

clearInterval(interval);

vm.run2();

} else {

clearInterval(interval);

vm.setData({

marqueeDistance2: -vm.data.windowWidth

});

vm.run2();

}

}

}, vm.data.interval);

}

})

到此,相信大家对"小程序怎么实现文字跑马灯效果"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report