微信小程序实现多行文字滚动效果
程序员文章站
2022-04-03 20:40:34
本文实例为大家分享了微信小程序实现多行文字滚动的具体代码,供大家参考,具体内容如下wxml
本文实例为大家分享了微信小程序实现多行文字滚动的具体代码,供大家参考,具体内容如下
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 }, })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。