image.png
布局頁面wxml
<view class="inform-warp">
<image class="inform" src='../images/stock/inform.png'></image>
<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>
</view>
樣式頁面 wxss
.inform-warp{
width: 100%;
background: #f8f8f8;
padding-left: 4%;
}
.inform{
width: 30rpx;
height: 28rpx;
margin-bottom: 32rpx;
display: inline-block;
}
.container {
height: 100%;
width: 90%;
display: flex;
flex-direction: column;
justify-content: space-between;
box-sizing: border-box;
padding: 0;
text-align: left;
display: inline-block;
}
.scrolltxt {
padding-left: 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頁面
Page({
data: {
text: "這是一條測(cè)試公告术瓮,看看效果怎么樣经伙,2019年3月23日",
marqueePace: 1,//滾動(dòng)速度
marqueeDistance: 0,//初始滾動(dòng)距離
marquee_margin: 30,
size:14,
interval: 20 // 時(shí)間間隔
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
onShow: function () {
var that = this;
var length = that.data.text.length * that.data.size;//文字長(zhǎng)度
var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕寬度
//console.log(length,windowWidth);
that.setData({
length: length,
windowWidth: windowWidth
});
that.scrolltxt();// 第一個(gè)字消失后立即從右邊出現(xiàn)
},
scrolltxt: function () {
var that = this;
var length = that.data.length;//滾動(dòng)文字的寬度
var windowWidth = that.data.windowWidth;//屏幕寬度
if (length > windowWidth){
var interval = setInterval(function () {
var maxscrollwidth = length + that.data.marquee_margin;//滾動(dòng)的最大寬度,文字寬度+間距辜御,如果需要一行文字滾完后再顯示第二行可以修改marquee_margin值等于windowWidth即可
var crentleft = that.data.marqueeDistance;
if (crentleft < maxscrollwidth) {//判斷是否滾動(dòng)到最大寬度
that.setData({
marqueeDistance: crentleft + that.data.marqueePace
})
}
else {
//console.log("替換");
that.setData({
marqueeDistance: 0 // 直接重新滾動(dòng)
});
clearInterval(interval);
that.scrolltxt();
}
}, that.data.interval);
}
else{
that.setData({ marquee_margin:"1000"});//只顯示一條不滾動(dòng)右邊間距加大伍派,防止重復(fù)顯示
}
}
})