微信小程序實現多行文字滾動效果

本文實例為大傢分享瞭微信小程序實現多行文字滾動的具體代碼,供大傢參考,具體內容如下

wxml

<view class="full" style="height:100%;overflow:hidden">
   <swiper autoplay="true" interval="3000" duration="500" circular="true" vertical="true" style="height:100%">
    <swiper-item wx:for="{{topnewslist}}" wx:key="{{index}}" catchtouchmove='catchTouchMove'>
     <van-notice-bar scrollable="{{false}}" bindtap="tonewsdetail" wx:for="{{item}}" wx:for-item="it" wx:key="{{index}}" data-newsid="{{it.new_id}}" style="width:100%" text="{{it.new_topic}}" />
    </swiper-item>
   </swiper>
</view>

wxss

swiper-item {
 height: 100%;
}

js

//index.js
//獲取應用實例
import api from "../../utils/api.js"
var token = ''
const app = getApp()

Page({
 data: {
  topnewslist:[]
 },
 onLoad: function () {
 },
 tonewsdetail(e){
  var newsid=e.currentTarget.dataset.newsid;
  wx.navigateTo({
   url: '/pages/newsdetail/newsdetail?newsid='+newsid,
  })
 },

 loadscrollnews(){
  api.get("mpapi/mpmnews.ashx", { action:'toplist',token:token}).then((res)=>{
   if(res.code==1){
   //res.list=[{"new_id":1,"new_topic":"111"},{"new_id":2,"new_topic":"222"},{"new_id":3,"new_topic":"333"},{"new_id":4,"new_topic":"444"}]
    this.setData({
     topnewslist: this.splitArr(res.list, 2) //調用
     //調用之後[[{"new_id":1,"new_topic":"111"},{"new_id":2,"new_topic":"222"}],[{"new_id":3,"new_topic":"333"},{"new_id":4,"new_topic":"444"}]]
    })
   }
  })
 },
 /**
  * 分割數組創建二維數組封裝
  * @param data 數組
  * @param senArrLen 需要分割成子數組的長度
  */
 splitArr(data, senArrLen){
  //處理成len個一組的數據
  let data_len = data.length;
  let arrOuter_len = data_len % senArrLen === 0 ? data_len / senArrLen : parseInt((data_len / senArrLen) + '') + 1;
  let arrSec_len = data_len > senArrLen ? senArrLen : data_len;//內層數組的長度
  let arrOuter = new Array(arrOuter_len);//最外層數組
  let arrOuter_index = 0;//外層數組的子元素下標
  // console.log(data_len % len);
  for (let i = 0; i < data_len; i++) {
   if (i % senArrLen === 0) {
    arrOuter_index++;
    let len = arrSec_len * arrOuter_index;
    //將內層數組的長度最小取決於數據長度對len取餘,平時最內層由下面賦值決定
    arrOuter[arrOuter_index - 1] = new Array(data_len % senArrLen);
    if (arrOuter_index === arrOuter_len)//最後一組
     data_len % senArrLen === 0 ?
      len = data_len % senArrLen + senArrLen * arrOuter_index :
      len = data_len % senArrLen + senArrLen * (arrOuter_index - 1);
    let arrSec_index = 0;//第二層數組的索引
    for (let k = i; k < len; k++) {//第一層數組的開始取決於第二層數組長度*當前第一層的索引
     arrOuter[arrOuter_index - 1][arrSec_index] = data[k];
     arrSec_index++;
    }
   }
  }
  return arrOuter
 },
 // 截獲豎向滑動
 catchTouchMove: function (res) {
  return false
 },
})

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: