微信小程序实现多行文字滚动效果

时间:2021-11-12 11:04:49

本文实例为大家分享了微信小程序实现多行文字滚动的具体代码,供大家参考,具体内容如下

wxml

?
1
2
3
4
5
6
7
<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

?
1
2
3
swiper-item {
 height: 100%;
}

js

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//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
 },
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Dilemma_me/article/details/103171614