作者:手机用户2502928867 | 来源:互联网 | 2023-08-22 01:13
效果
wxml
<view>1 显示完后再显示view>
<view class&#61;"example"><view class&#61;"marquee_box"><view class&#61;"marquee_text" style&#61;"{{orientation}}:{{marqueeDistance}}px;font-size: {{size}}px;">{{text}}view>view>
view>
<view>2 出现白边后即显示view>
<view class&#61;"example"><view class&#61;"marquee_box"><view class&#61;"marquee_text" style&#61;"{{orientation}}:{{marqueeDistance2}}px;font-size: {{size}}px;"><text>{{text}}text><text wx:if&#61;"{{marquee2copy_status}}" style&#61;"margin-left:{{marquee2_margin}}px;">{{text}}text>view>view>
view>
wxss
.example {display: block;width: 100%;height: 100rpx;
}.marquee_box {width: 100%;position: relative;
}.marquee_text {white-space: nowrap;position: absolute;top: 0;
}
js
Page({data: {text: &#39;这是一条会滚动的文字滚来滚去的文字跑马灯&#xff0c;哈哈哈哈哈哈哈哈&#39;,marqueePace: 1,marqueeDistance: 0,marqueeDistance2: 0,marquee2copy_status: false,marquee2_margin: 60,size: 14,orientation: &#39;left&#39;,interval: 20 },onShow: function () {var vm &#61; this;var length &#61; vm.data.text.length * vm.data.size;var windowWidth &#61; 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 &#61; this;var interval &#61; 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 &#61; this;var interval &#61; setInterval(function () {if (-vm.data.marqueeDistance2 < vm.data.length) {vm.setData({marqueeDistance2: vm.data.marqueeDistance2 - vm.data.marqueePace,marquee2copy_status: vm.data.length &#43; vm.data.marqueeDistance2 <&#61; vm.data.windowWidth &#43; vm.data.marquee2_margin,});} else {if (-vm.data.marqueeDistance2 >&#61; 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);}
})