wxml:
<view class="example swiper_container">
<view class="marquee_box">
<view class="marquee_text" style="{{orientation}}:{{marqueeDistance}}px;font-size: {{size}}px;">
<view class="swiper_item" bindtap="detailBtn">
<!-- <rich-text nodes="{{inform.content}}"></rich-text> -->
<text>{{text}}</text>
</view>
<view wx:if="{{marquee2copy_status}}" bindtap="detailBtn" style="margin-left:{{marquee2_margin}}rpx;">
<text>{{text}}</text>
</view>
</view>
</view>
</view>
wxss:
.swiper_container {
height: 37rpx;
font-size: 26rpx;
font-weight: 300;
color: #f8b233;
width: 570rpx;
overflow: hidden;
}
.swiper_item {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.example {
display: block;
width: 100%;
height: 48rpx;
}
.marquee_box {
width: 100%;
position: relative;
}
.marquee_text {
white-space: nowrap;
position: absolute;
top: 0;
}
index.js:
Page({
data: {
// 走马灯
text: '初始化文本',
marqueePace: 0.5, //滚动速度
marqueeDistance: 0, //初始滚动距离
marquee2copy_status: false,
marquee2_margin: 60,
size: 14,
orientation: 'left', //滚动方向
interval: 20, // 时间间隔
......
},
// 跳转到 详情页
detailBtn: function() {
wx.navigateTo({
url: "../warnDetails/warnDetails?recNo=" + this.data.inform.recNo
})
},
// 动起来
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.run2(); // 第一个字消失后立即从右边出现
},
run2: function() {
var vm = this;
var interval = setInterval(function() {
if (-vm.data.marqueeDistance < vm.data.length) {
// 如果文字滚动到出现marquee2_margin=30px的白边,就接着显示
vm.setData({
marqueeDistance: vm.data.marqueeDistance - vm.data.marqueePace,
marquee2copy_status: vm.data.length + vm.data.marqueeDistance <= vm.data.windowWidth + vm.data.marquee2_margin,
});
} else {
if (-vm.data.marqueeDistance >= vm.data.marquee2_margin) { // 当第二条文字滚动到最左边时
vm.setData({
marqueeDistance: vm.data.marquee2_margin // 直接重新滚动
});
clearInterval(interval);
vm.run2();
} else {
clearInterval(interval);
vm.setData({
marqueeDistance: -vm.data.windowWidth
});
vm.run2();
}
}
}, vm.data.interval);
}
})