亲宝软件园·资讯

展开

微信小程序滚动条功能

kleinBlue. 人气:0

view

<view class="conty">
  <!-- 滚动字幕 -->
<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>

js

data: {
  text: "这是一条农协动态,请您仔细阅读,若内容有所问题,请联系客服!",
  marqueePace: 1,//滚动速度
  marqueeDistance: 0,//初始滚动距离
  marquee_margin: 30,
  size:14,
  interval: 20 ,// 时间间隔
  HomeIndex:0,
  },
 onShow: function () {
  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;//屏幕宽度
  if (length > windowWidth){
   var interval = 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 {
    //console.log("替换");
    that.setData({
    marqueeDistance: 0 // 直接重新滚动
    });
    clearInterval(interval);
    that.scrolltxt();
   }
   }, that.data.interval);
  }
  else{
   that.setData({ marquee_margin:"1000"});//只显示一条不滚动右边间距加大,防止重复显示
  } 
  }

css

.scrolltxt{
  width: 100%;
  padding:0 20rpx;
  background:#2a4d69;
}
.marquee_box {
  position:relative;
  color:white;
  height:90rpx;display:
  block;overflow:hidden;
} 
.marquee_text {
  white-space: nowrap;
  position:absolute;
  top:0;
  font-size:14px;
  height:90rpx;
  line-height:90rpx;
}

加载全部内容

相关教程
猜你喜欢
用户评论