当前位置:   article > 正文

小程序实现左右滑动切换菜单+瀑布流显示列表_前端瀑布流+左右滑动tab页面

前端瀑布流+左右滑动tab页面

小程序实现左右滑动列表区域,切换菜单(横向滑动菜单,切换时自动设置选中的菜单窗口居中显示),加载显示对应瀑布流数据,页面滚动时,设置菜单栏是否置顶显示

自己写的一个demo(下载地址:https://download.csdn.net/download/qq_16494241/11208785

 

 

app.js中加处理方法:

  1. //处理下页列表数据,追加至列表数据存储变量中
  2. addList(oldData, newData) {
  3. for (var item in newData) {
  4. oldData.push(newData[item]);
  5. }
  6. return oldData;
  7. },
  8. //处理设置最新获取到的瀑布流数据
  9. setCurNewPubuImgData: function (oldImgDataJson) {
  10. let oldImgData = [];
  11. for (var item in oldImgDataJson) {
  12. oldImgData[item] = oldImgDataJson[item];
  13. }
  14. return oldImgData;
  15. },
  16. //处理存储瀑布流左右两边数据
  17. setCurResultsPubuImgData(newImgData, oldData, leftH, rightH ,callback) {
  18. //let leftH = 0;
  19. //let rightH = 0;
  20. let resultsList = {
  21. listL: [],
  22. listR: [],
  23. }
  24. let leftStart = 0;
  25. let rightStart = 0;
  26. for (var item in newImgData) {
  27. //console.log(leftH, rightH, newImgData[item].h);
  28. //console.log(leftH <= rightH);
  29. if (leftH <= rightH){
  30. resultsList.listL[leftStart] = oldData[item];
  31. leftStart ++;
  32. leftH += newImgData[item].h;
  33. }else{
  34. resultsList.listR[rightStart] = oldData[item];
  35. rightStart ++;
  36. rightH += newImgData[item].h;
  37. }
  38. }
  39. callback(resultsList , leftH, rightH);
  40. }

index.wxml

  1. <!--pages/test/index.wxml-->
  2. <scroll-view class="body" scroll-y="{{ifCanScroll}}" bindscrolltolower="scrollEnd" bindscroll="scrollIn">
  3. <!--KV图切换-->
  4. <view id='top' class='swiper'>
  5. <swiper indicator-color='{{indicatorcolor}}' indicator-active-color='{{indicatoractivecolor}}' indicator-dots="{{indicatordots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  6. <block wx:for="{{swiperImgArr}}">
  7. <swiper-item>
  8. <navigator wx:if="{{item.linkUrl != ''}}" url="{{item.linkUrl}}" open-type="navigate">
  9. <image mode='widthFix' src="{{item.src}}" />
  10. </navigator>
  11. <image wx:else mode='widthFix' src="{{item.src}}" />
  12. </swiper-item>
  13. </block>
  14. </swiper>
  15. </view>
  16. <!--选择切换菜单 与以下 分开写避免了导航固定时页面跳动-->
  17. <scroll-view scroll-x scroll-with-animation="true" scroll-left="{{ navIndex * 150 - 375 + 150 / 2 }}rpx" class="nav_box">
  18. <view class="nav_body nav_body_cur{{navIndex}}" style="width: calc(150rpx * {{navList.length}});">
  19. <view class='nav_li' wx:for="{{navList}}" data-type='{{index}}' bindtap='changeTitle'>{{item}}</view>
  20. </view>
  21. </scroll-view>
  22. <!--浮动在顶部的选择切换菜单 与以上 分开写避免了导航固定时页面跳动-->
  23. <scroll-view scroll-x scroll-with-animation="true" scroll-left="{{ navIndex * 150 - 375 + 150 / 2 }}rpx" class="nav_box nav_body_top" style="height: {{ifShowTopNav ? 'auto' : 0}};">
  24. <view class="nav_body nav_body_cur{{navIndex}}" style="width: calc(150rpx * {{navList.length}});">
  25. <view class='nav_li' wx:for="{{navList}}" data-type='{{index}}' bindtap='changeTitle'>{{item}}</view>
  26. </view>
  27. </scroll-view>
  28. <!--瀑布流所需,图片显示前用来获取图片等宽时的高度-->
  29. <view class='get_pubu_img'>
  30. <image wx:for="{{pubuliuNewArrData}}" mode='widthFix' data-key='{{index}}' bindload='pubuImgLoad' src="{{item.src}}" />
  31. </view>
  32. <!--瀑布流显示-->
  33. <view class="box" style='{{moveLeft}}' bindtouchstart='touchStart' bindtouchmove='touchMove' bindtouchend='touchEnd'>
  34. <view wx:if="{{!pubuliuResultsList.listL[0]}}" class="no_data_show">暂无数据</view>
  35. <!--瀑布流左边数据-->
  36. <view class='box_l'>
  37. <navigator wx:for="{{pubuliuResultsList.listL}}" class="list" url="{{item.linkUrl}}" open-type="navigate" hover-class="none">
  38. <image mode='widthFix' src="{{item.src}}" />
  39. <view class='list_title'>{{item.title}}</view>
  40. <view class='list_user f_24'>
  41. <image src='{{item.userhead}}' mode='widthFix'></image>
  42. <text class='list_user_name'>{{item.nickname}}</text>
  43. <text class='list_zan' data-id='{{item.id}}' data-ifzan='{{item.ifzan}}' data-index='{{index}}' data-side='listL' catchtap='dianzan'>
  44. <text wx:if="{{item.ifzan}}" class='iconfont icon-xinheart280 f_30'></text>
  45. <text wx:esle class='iconfont icon-xinheart281 f_30'></text>
  46. </text>
  47. <text>{{item.zannum}}</text>
  48. </view>
  49. <text>{{item.key}}</text>
  50. </navigator>
  51. </view>
  52. <!--瀑布流右边数据-->
  53. <view class='box_r'>
  54. <navigator wx:for="{{pubuliuResultsList.listR}}" class="list" url="{{item.linkUrl}}" open-type="navigate" hover-class="none">
  55. <image mode='widthFix' src="{{item.src}}" />
  56. <view class='list_title'>{{item.title}}</view>
  57. <view class='list_user f_24'>
  58. <image src='{{item.userhead}}' mode='widthFix'></image>
  59. <text class='list_user_name'>{{item.nickname}}</text>
  60. <text class='list_zan' data-id='{{item.id}}' data-ifzan='{{item.ifzan}}' data-index='{{index}}' data-side='listR' catchtap='dianzan'>
  61. <text wx:if="{{item.ifzan}}" class='iconfont icon-xinheart280 f_30'></text>
  62. <text wx:esle class='iconfont icon-xinheart281 f_30'></text>
  63. </text>
  64. <text>{{item.zannum}}</text>
  65. </view>
  66. <text>{{item.key}}</text>
  67. </navigator>
  68. </view>
  69. </view>
  70. <view wx:if="{{!ifHaveMore && pubuliuResultsList.listL[0]}}" class='showNoMore'>我是有底线哒</view>
  71. </scroll-view>

index.wxss

  1. /* pages/test/index.wxss */
  2. @font-face {font-family: "iconfont";
  3. src: url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAMMAAsAAAAABygAAAK/AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCCfgqCDIIMATYCJAMMCwgABCAFhG0HQxtXBhEVlBtkPxJs3OJiZX3TRZXQIr0Jolr7t2d3PwAqRrAYUECawX+ViooCQPUqGvgaIIAA7+V0DBejXqgsxfV861k+45OO+X3WeZxGKYRESIw4nDbF5wPlsuawOh8TMMGAxtoUWenExLxleK1L6aA2QwBnwiWQiqqGDhwxkF4CyMLs9CiOOT2mJVNwRLCvmWiQo9jhqPapu8CR8PHylbQcQWGnIQNbpionKLHdsd3Vu6xdDPgEBPNZAXUaaCABGJCuWnsLOo4n0DjbcUME9tVKwHbXsrDd8fm7/cMDQYHYQ9JdAYgBmbojAZdzawKYggJAA2AfJxuAbUz1HSio4ODA0auxufnX4190v2BPeFqef+54/rFjV9izZ53PagfMvfjSGRv/4lPnrtCnTztopKHpeddzfMipa2k7qw4OqzutZfVNHd2rN/za7yU6fU7Ye/E0vKOEfU5vE2/6t6/e6Oiub6JbA2fleNt+4egxrbnGfiEdEH3Ft2H+fGtHXUNz6eHDwAk5mJOf69twhREArP36vV7HvxBA4HHB2r9hj4Jvji4GgFdub34jn7Xf0N+I4805BH60oGeZcUTEoI3F53zPqUkbaVUAXM2FBXF7ez9e6dc7HQRH/FIoHAiDxpEIoqEmwA4XMmCPI8XQOotXTXfhg+zJnIA4bwTh6QPF3S1onr5EQ/3Bjr8/7Hm2h7OufBZ0EVUjygkdYwT/giZMY2ImFlHmEfOtd5QVhbQr0tqEwEhdTFU4Is0xZL1ny5wg0TRAibbDvp9gpqnFwLJmngulUt2NZJiGjVCOIIehCHgvoBFMRsmvzUTp/kcou+k51JLVZr9CZNXUDwxJ9yBW8rFX1r1cs7rLLMYSkJDJAJSoHer1JmCub9dCAZPqEdFZQdFqqa9crq8cPiBPzYN8VadEi8HlzVijI7YHndrMZgMA') format('woff2'),
  4. url('//at.alicdn.com/t/font_1209810_s2iq0eomwkc.woff?t=1558690554699') format('woff'),
  5. url('//at.alicdn.com/t/font_1209810_s2iq0eomwkc.ttf?t=1558690554699') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  6. url('//at.alicdn.com/t/font_1209810_s2iq0eomwkc.svg?t=1558690554699#iconfont') format('svg'); /* iOS 4.1- */
  7. }
  8. .iconfont {
  9. font-family: "iconfont" !important;
  10. font-style: normal;
  11. -webkit-font-smoothing: antialiased;
  12. -moz-osx-font-smoothing: grayscale;
  13. }
  14. .icon-xinheart280:before {
  15. content: "\e9cb";
  16. }
  17. .icon-xinheart281:before {
  18. content: "\e9cc";
  19. }
  20. page {
  21. height: 100%;
  22. font-size: 26rpx;
  23. line-height: 1.5;
  24. }
  25. image{ vertical-align: middle;}
  26. .nav_box{ width: 100%; background-color: #fff;}
  27. .nav_box.nav_body_top{ overflow: hidden; position: fixed; left: 0; top: 0; z-index: 999;}
  28. .nav_body{ width: auto; padding: 22rpx 0; margin: 0 auto; position: relative;
  29. display: flex; justify-content: flex-start; align-items: center;
  30. }
  31. .nav_body:before{ content: ""; width: 100%; min-width: 750rpx; height: 1px; background-color: #ddd; position: absolute; left: 50%; bottom: 0;
  32. -webkit-transform: translateX(-50%);
  33. transform: translateX(-50%);
  34. }
  35. .nav_body:after{ content: ""; width: 124rpx; height: 1.5px; background-color: #fc2549; position: absolute; left: ; bottom: 0;
  36. -webkit-transition:all linear .3s;
  37. transition:all linear .3s;
  38. }
  39. .nav_li{ width: 150rpx; height: 52rpx; line-height: 52rpx; text-align: center; color: #000; font-size: 26rpx; position: relative;
  40. -webkit-transition:all linear .3s;
  41. transition:all linear .3s;
  42. }
  43. .nav_li:not(:first-child):after{ content: ""; width: 1px; height: 100%; background-color: #ddd; position: absolute; left: 0; top: 0;}
  44. .nav_body.nav_body_cur0 .nav_li:nth-child(1) ,
  45. .nav_body.nav_body_cur1 .nav_li:nth-child(2) ,
  46. .nav_body.nav_body_cur2 .nav_li:nth-child(3) ,
  47. .nav_body.nav_body_cur3 .nav_li:nth-child(4) ,
  48. .nav_body.nav_body_cur4 .nav_li:nth-child(5) ,
  49. .nav_body.nav_body_cur5 .nav_li:nth-child(6) ,
  50. .nav_body.nav_body_cur6 .nav_li:nth-child(7) ,
  51. .nav_body.nav_body_cur7 .nav_li:nth-child(8) ,
  52. .nav_body.nav_body_cur8 .nav_li:nth-child(9) ,
  53. .nav_body.nav_body_cur9 .nav_li:nth-child(10){ color: #fc2549; font-weight: bold;}
  54. .nav_body.nav_body_cur0:after{ left: calc((150rpx - 124rpx) / 2);}
  55. .nav_body.nav_body_cur1:after{ left: calc(150rpx + (150rpx - 124rpx) / 2);}
  56. .nav_body.nav_body_cur2:after{ left: calc(150rpx * 2 + (150rpx - 124rpx) / 2);}
  57. .nav_body.nav_body_cur3:after{ left: calc(150rpx * 3 + (150rpx - 124rpx) / 2);}
  58. .nav_body.nav_body_cur4:after{ left: calc(150rpx * 4 + (150rpx - 124rpx) / 2);}
  59. .nav_body.nav_body_cur5:after{ left: calc(150rpx * 5 + (150rpx - 124rpx) / 2);}
  60. .nav_body.nav_body_cur6:after{ left: calc(150rpx * 6 + (150rpx - 124rpx) / 2);}
  61. .nav_body.nav_body_cur7:after{ left: calc(150rpx * 7 + (150rpx - 124rpx) / 2);}
  62. .nav_body.nav_body_cur8:after{ left: calc(150rpx * 8 + (150rpx - 124rpx) / 2);}
  63. .nav_body.nav_body_cur9:after{ left: calc(150rpx * 9 + (150rpx - 124rpx) / 2);}
  64. .nav_body.nav_body_cur10:after{ left: calc(150rpx * 10 + (150rpx - 124rpx) / 2);}
  65. .showNoMore{ padding: 20rpx 0; text-align: center; color: #aaa;}
  66. .showNoMore:before{ content: ""; display: inline-block; vertical-align: middle; width: 150rpx; height: 1px; margin: 0 15rpx; background-color: #ddd;}
  67. .showNoMore:after{ content: ""; display: inline-block; vertical-align: middle; width: 150rpx; height: 1px; margin: 0 15rpx; background-color: #ddd;}
  68. .body{ height: 100%; background-color: #f4f8fb;}
  69. .swiper{ padding: 0 20rpx; background-color: #fff;}
  70. swiper{ height: 350rpx; margin: 0 auto; overflow: hidden;}
  71. swiper image{ width: 100%;}
  72. /* 列表 */
  73. .get_pubu_img{ display: none;}
  74. .box{ margin-top: 20rpx; display: flex; flex-wrap: wrap; flex-direction: row; justify-content:flex-start; align-items: flex-start; position: relative;}
  75. .no_data_show{ width: 100%; min-height: calc(100vh - 500rpx); margin: 0 auto; text-align: center; font-size: 36rpx; font-weight: bold; background-color: #fff; display: flex; justify-content: center; align-items: center;}
  76. .box_l , .box_r{ width: 345rpx; margin-left: 20rpx;}
  77. .list{ width:100%; margin-bottom: 20rpx; padding-bottom: 10rpx; border-radius: 10rpx; background-color: #fff; position: relative; overflow: hidden;
  78. -webkit-box-shadow: 2rpx 5rpx 10rpx rgba(0,0,0,.1);
  79. box-shadow: 2rpx 5rpx 10rpx rgba(0,0,0,.1);
  80. }
  81. .list > image{ width: 100%;/* border-top-left-radius: 10rpx; border-top-right-radius: 10rpx;*/}
  82. .list_title{ height: 64rpx; line-height: 64rpx; padding: 0 20rpx; color: #4c4c4c; overflow: hidden; white-space:nowrap; text-overflow: ellipsis;}
  83. .list_user{ height: 70rpx; padding: 0 20rpx; color: #989898;
  84. display: flex; flex-wrap: nowrap; justify-content: flex-start; align-items: center;
  85. }
  86. .list_user image{ width: 50rpx; margin-right: 10rpx; border-radius: 50%;}
  87. .list_user_name{ width: 150rpx; margin-right: 10rpx; overflow: hidden; white-space:nowrap; text-overflow: ellipsis;}
  88. .list_zan{ display: inline-block; vertical-align: middle; width: 30rpx; height: 30rpx; margin-right: 10rpx; position: relative; top: -4rpx;}
  89. .list_zan .iconfont{ width: 30rpx; height: 3rpx; position: absolute; left: 0; top: 0;}

index.js

  1. // pages/test/index.js
  2. var app = getApp()
  3. var leftHstart = 0, rightHstart = 0; //加载下页瀑布流数据时,前面数据的左右盒子高度
  4. var newImgData = []; //处理瀑布流所需变量
  5. var canLoadNextPage = false;
  6. var scrollTopH; //获取顶部高度,用于设置浮动顶部的菜单(不同尺寸手机,高度不同)
  7. var touchLRnum = 100; //列表区域左右滑动的距离(切换菜单的值)
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. navList: ['首页','菜单1','菜单2','菜单3','菜单444','菜单555','菜单666','菜单777','菜单8','菜单9','菜单10'],
  14. navIndex: 0, //当前选择的菜单key(对应 navList[key] 菜单内容)
  15. ifShowTopNav: false, //是否显示浮动的顶部菜单
  16. pageSize: 1, //页码
  17. ifHaveMore: true, //是否还有下一页
  18. moveLeft: 'left:0px;', //列表处左右滑动时位移值
  19. ifCanScroll: true, //列表处左右滑动时设为false,这样页面不会滚动影响页面体验
  20. //KV图相关配置
  21. indicatordots: true,
  22. indicatorcolor: '#989898',
  23. indicatoractivecolor: '#fc2549',
  24. autoplay: true,
  25. interval: 3000,
  26. duration: 500,
  27. //swiperImgArr: [], //KV图数据
  28. //列表
  29. //pubuliuNewArrData:[], //最新一次获取到的 瀑布流JSON数据 转成 的 数组
  30. //pubuliuResultsList:{
  31. // listL: [],
  32. // listR: [],
  33. //}
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad: function (options) {
  39. let This = this;
  40. This.getKVimg(); //获取 KV图 列表
  41. This.initFun(); //初始化 / 清空 页面数据
  42. This.getListData(); //获取页面列表数据
  43. },
  44. /**
  45. * 生命周期函数--监听页面显示
  46. */
  47. onShow: function () {
  48. //获取顶部高度,用于设置浮动顶部的菜单(不同尺寸手机,高度不同)
  49. //延迟执行是为了防止导航栏以上有些元素未加载成功会影响高度计算(最好是在导航栏以上元素请求加载成功后处执行以下代码)
  50. setTimeout(function(){
  51. let query = wx.createSelectorQuery();
  52. query.select('#top').boundingClientRect()
  53. query.exec(function (res) {
  54. scrollTopH = res[0].height;
  55. })
  56. },1500);
  57. },
  58. /**
  59. * 生命周期函数--监听页面初次渲染完成
  60. */
  61. onReady: function () {
  62. },
  63. /**
  64. * 生命周期函数--监听页面隐藏
  65. */
  66. onHide: function () {
  67. },
  68. /**
  69. * 生命周期函数--监听页面卸载
  70. */
  71. onUnload: function () {
  72. },
  73. /**
  74. * 页面相关事件处理函数--监听用户下拉动作
  75. */
  76. onPullDownRefresh: function () {
  77. },
  78. /**
  79. * 用户点击右上角分享
  80. */
  81. onShareAppMessage: function () {
  82. },
  83. //初始化 / 清空 页面数据
  84. initFun: function () {
  85. let This = this;
  86. leftHstart = 0, rightHstart = 0; //加载下页瀑布流数据时,前面数据的左右盒子高度
  87. newImgData = []; //处理瀑布流所需变量
  88. This.setData({
  89. //swiperImgArr: '',
  90. //navIndex: 0,
  91. pageSize: 1, //页码
  92. pubuliuNewArrData: '',
  93. pubuliuResultsList: '',
  94. });
  95. },
  96. //获取 KV图 列表
  97. getKVimg: function () {
  98. let This = this;
  99. This.setData({
  100. swiperImgArr: [
  101. { src: 'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640', linkUrl: '' },
  102. { src: 'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640', linkUrl: '' },
  103. { src: 'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640', linkUrl: '' },
  104. ],
  105. });
  106. },
  107. //设置顶部固定菜单定位
  108. // onPageScroll:function(e){ // 获取滚动条当前位置
  109. // console.log(e.scrollTop)
  110. scrollIn: function (e) {
  111. let This = this;
  112. if (e.detail.scrollTop >= scrollTopH) {
  113. if (!This.data.ifShowTopNav) {
  114. This.setData({
  115. ifShowTopNav: true
  116. });
  117. }
  118. } else {
  119. if (This.data.ifShowTopNav) {
  120. This.setData({
  121. ifShowTopNav: false
  122. });
  123. }
  124. }
  125. },
  126. //左右滑动 切换菜单
  127. touchStart: function (e) {
  128. if (e.touches.length == 1) {
  129. let This = this;
  130. This.setData({
  131. startX: e.touches[0].clientX,
  132. startY: e.touches[0].clientY,
  133. });
  134. }
  135. },
  136. touchMove: function (e) {
  137. if (e.touches.length == 1) {
  138. let This = this;
  139. let moveX = e.touches[0].clientX;
  140. let diffX = This.data.startX - moveX;
  141. let moveY = e.touches[0].clientY;
  142. let diffY = This.data.startY - moveY;
  143. let moveLeft = 'left:0px;';
  144. if (Math.abs(diffY) < Math.abs(diffX)) {
  145. if (diffX < 0 && This.data.navIndex > 0) { //向右
  146. //moveLeft = 'left:' + -(diffX < touchLRnum ? -touchLRnum : diffX) + 'px;';
  147. moveLeft = 'left:' + -diffX + 'px;';
  148. } else if (diffX > 0 && This.data.navIndex < This.data.navList.length - 1) { //向左
  149. //moveLeft = 'left:-' + (diffX > touchLRnum ? touchLRnum : diffX) + 'px;';
  150. moveLeft = 'left:-' + diffX + 'px;';
  151. }
  152. This.setData({
  153. ifCanScroll: false, //列表处左右滑动时设为false,这样页面不会滚动影响页面体验
  154. });
  155. }else{
  156. This.setData({
  157. ifCanScroll: true, //列表处左右滑动时设为false,这样页面不会滚动影响页面体验
  158. });
  159. }
  160. This.setData({
  161. moveLeft: moveLeft
  162. });
  163. }
  164. },
  165. touchEnd: function (e) {
  166. if (e.changedTouches.length == 1) {
  167. let This = this;
  168. let endX = e.changedTouches[0].clientX;
  169. let diffX = This.data.startX - endX;
  170. let endY = e.changedTouches[0].clientY;
  171. let diffY = This.data.startY - endY;
  172. let moveLeft = 'left:0px;';
  173. if (Math.abs(diffY) < Math.abs(diffX)) {
  174. if (diffX < 0 && diffX < -touchLRnum && This.data.navIndex > 0) { //向右
  175. let moveEndNavIndex = This.data.navIndex - 1;
  176. This.setData({
  177. navIndex: moveEndNavIndex
  178. });
  179. This.initFun(); //初始化 / 清空 页面数据
  180. This.getListData(); //获取页面列表数据
  181. } else if (diffX > 0 && diffX > touchLRnum && This.data.navIndex < This.data.navList.length - 1) { //向左
  182. let moveEndNavIndex = This.data.navIndex + 1;
  183. This.setData({
  184. navIndex: moveEndNavIndex
  185. });
  186. This.initFun(); //初始化 / 清空 页面数据
  187. This.getListData(); //获取页面列表数据
  188. }
  189. }
  190. This.setData({
  191. moveLeft: moveLeft,
  192. ifCanScroll: true, //列表处左右滑动时设为false,这样页面不会滚动影响页面体验
  193. });
  194. }
  195. },
  196. //选择 切换菜单
  197. changeTitle: function (e) {
  198. let chooseNav = e.currentTarget.dataset.type;
  199. let This = this;
  200. if (chooseNav != This.data.navIndex){
  201. This.setData({
  202. navIndex: chooseNav,
  203. });
  204. This.initFun(); //初始化 / 清空 页面数据
  205. This.getListData(); //获取页面列表数据
  206. }
  207. },
  208. /**
  209. * 页面上拉触底事件的处理函数
  210. */
  211. // onReachBottom: function () {
  212. scrollEnd: function () { //滚动到底部加载更多
  213. let This = this;
  214. if (This.data.ifHaveMore) {
  215. //console.log('加载一下页');
  216. if (!canLoadNextPage) {
  217. return;
  218. }
  219. newImgData = [];
  220. This.setData({
  221. pubuliuNewArrData: '',
  222. pageSize: This.data.pageSize + 1
  223. });
  224. This.getListData(); //获取页面列表数据
  225. }
  226. },
  227. //获取页面列表数据
  228. getListData: function () {
  229. let This = this;
  230. wx.showLoading({
  231. title: '加载中',
  232. mask:true
  233. })
  234. //查询数据 所需参数
  235. let dataJson = {
  236. type: This.data.navIndex,
  237. page: This.data.pageSize, //页码
  238. }
  239. /*test*/
  240. let res = {
  241. status: 1,
  242. ifHaveMore: This.data.pageSize > 2 ? false : true, //是否还有下一页
  243. list: {
  244. 0: {
  245. id: '001',
  246. title: This.data.navList[This.data.navIndex] + ' - ' + This.data.pageSize + '页 - 1111111',
  247. userhead: 'https://iconfont.alicdn.com/t/1550898018467.jpeg@100h_100w.jpg',
  248. nickname: '昵称昵称昵称111',
  249. zannum: 88,
  250. ifzan: true,
  251. src: "https://img.t.sinajs.cn/t5/skin/public/profile_cover/001_s.jpg?version=b97b8caee54a6c78",
  252. linkUrl: '/pages/detail/detail'
  253. },
  254. 1: {
  255. id: '002',
  256. title: This.data.pageSize + '页222222222222',
  257. userhead: 'https://avatar.csdn.net/D/5/3/3_qq_16494241.jpg',
  258. nickname: '昵称昵称昵称222',
  259. zannum: 88,
  260. ifzan: false,
  261. src: "https://img2018.cnblogs.com/news/24442/201902/24442-20190214124232223-959977867.jpg",
  262. linkUrl: '/pages/detail/detail'
  263. },
  264. 2: {
  265. id: '003',
  266. title: This.data.pageSize + '页33333333333333',
  267. userhead: 'https://avatar.csdn.net/D/5/3/3_qq_16494241.jpg',
  268. nickname: '昵称昵称昵称333',
  269. zannum: 88,
  270. ifzan: false,
  271. src: "https://s3.ifanr.com/wp-content/uploads/2018/08/211.jpg!720",
  272. linkUrl: '/pages/detail/detail'
  273. },
  274. 3: {
  275. id: '004',
  276. title: This.data.pageSize + '页44444444444444',
  277. userhead: 'https://iconfont.alicdn.com/t/1550898018467.jpeg@100h_100w.jpg',
  278. nickname: '昵称昵称昵称444',
  279. zannum: 88,
  280. ifzan: true,
  281. src: "https://t1.hddhhn.com/uploads/tu/201512/14/87.jpg",
  282. linkUrl: '/pages/detail/detail'
  283. },
  284. }
  285. }
  286. if (res.status == 1) {
  287. This.setData({
  288. ifHaveMore: res.ifHaveMore, //是否还有下一页
  289. pubuliuNewArrData: app.setCurNewPubuImgData(res.list) //处理设置最新获取到的瀑布流数据
  290. });
  291. }
  292. setTimeout(function () {
  293. wx.hideLoading();
  294. }, 500)
  295. /*test*/
  296. return;
  297. //请求获取数据
  298. wx.request({
  299. url: '获取列表数据', //仅为示例,并非真实的接口地址
  300. data: dataJson,
  301. header: {
  302. 'content-type': 'application/json' // 默认值
  303. },
  304. success(res) {
  305. wx.hideLoading();
  306. }
  307. })
  308. },
  309. /*
  310. 瀑布流相关处理:
  311. 1、处理最新加载的瀑布流数据中图片,先显示,再根据 bindload 获取 并 存储 与 原来KEY 相对应的 等宽情况下的高度 数组
  312. => newImgData[key].h (key 与最新加载的JSON数据的 key 相同)
  313. 2、根据所有图片加载完成后,调用app.js中方法,设置左、右两边数据
  314. */
  315. pubuImgLoad: function (e) {
  316. let This = this;
  317. let inListIndex = e.currentTarget.dataset.key;
  318. //console.log(e.detail.width);
  319. //console.log(inListIndex);
  320. newImgData[inListIndex] = {};
  321. newImgData[inListIndex].h = (300 / e.detail.width) * e.detail.height;
  322. if (newImgData.length == This.data.pubuliuNewArrData.length) {
  323. //防止最后一个数据中的图片先加载完成,这样lenth也相等
  324. for (let i = 0; i < newImgData.length; i++) {
  325. if (!newImgData[i]) {
  326. return;
  327. }
  328. }
  329. //newImgData 获取的最新数据 - newImgData[key].h (key 与最新加载的JSON数据的 key 相同)
  330. //This.data.pubuliuNewArrData 获取的最新数据(处理过的数组) - This.data.pubuliuNewArrData[key]. (key 与最新加载的JSON数据的 key 相同)
  331. //leftHstart 本次数据加载 前 的 左 边高度
  332. //rightHstart 本次数据加载 前 的 右 边高度
  333. app.setCurResultsPubuImgData(newImgData, This.data.pubuliuNewArrData, leftHstart, rightHstart, function (pubuliuResultsList, leftH, rightH) {
  334. //console.log(pubuliuResultsList);
  335. leftHstart = leftH;
  336. rightHstart = rightH;
  337. if (This.data.pubuliuResultsList) {
  338. pubuliuResultsList.listL = This.data.pubuliuResultsList.listL.concat(pubuliuResultsList.listL);
  339. pubuliuResultsList.listR = This.data.pubuliuResultsList.listR.concat(pubuliuResultsList.listR);
  340. }
  341. This.setData({
  342. pubuliuResultsList: pubuliuResultsList
  343. });
  344. setTimeout(function () {
  345. canLoadNextPage = true;
  346. }, 500);
  347. })
  348. }
  349. },
  350. // 点赞、取消赞
  351. dianzan: function (e) {
  352. // console.log(e.currentTarget.dataset.id); //对应数据库ID 用来更新数据库中对应 赞 的
  353. // console.log(e.currentTarget.dataset.ifzan); //当前用户是否赞过
  354. // console.log(e.currentTarget.dataset.side); //listL(左边数据) | listR(右边数据)
  355. // console.log(e.currentTarget.dataset.index); //对应当前页面key(左、右边) 用来更新页面上显示样式的
  356. let This = this;
  357. wx.showLoading({
  358. title: '点赞中',
  359. mask:true
  360. })
  361. //查询数据 所需参数
  362. let dataJson = {
  363. id: e.currentTarget.dataset.id, //对应数据库ID 用来更新数据库中对应 赞 的
  364. ifzan: e.currentTarget.dataset.ifzan, //当前用户是否赞过
  365. }
  366. /*test*/
  367. let res = {
  368. status: 1,
  369. zannum:This.data.pubuliuResultsList[e.currentTarget.dataset.side][e.currentTarget.dataset.index].zannum == 88 ? 999 : 88 , //点赞、取消赞后 的 赞数量
  370. ifzan: !dataJson.ifzan,
  371. info: '是否成功提示文本'
  372. }
  373. if (res.status == 1) {
  374. let tempData = This.data.pubuliuResultsList;
  375. tempData[e.currentTarget.dataset.side][e.currentTarget.dataset.index].ifzan = res.ifzan;
  376. tempData[e.currentTarget.dataset.side][e.currentTarget.dataset.index].zannum = res.zannum;
  377. This.setData({
  378. pubuliuResultsList: tempData
  379. });
  380. } else {
  381. }
  382. setTimeout(function(){
  383. wx.showToast({
  384. title: res.info,
  385. icon: 'success',
  386. duration: 800
  387. })
  388. //wx.hideLoading();
  389. },500)
  390. /*test*/
  391. return;
  392. //请求获取数据
  393. wx.request({
  394. url: '获取列表数据', //仅为示例,并非真实的接口地址
  395. data: dataJson,
  396. header: {
  397. 'content-type': 'application/json' // 默认值
  398. },
  399. success(res) {
  400. }
  401. })
  402. },
  403. })

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/565654
推荐阅读
相关标签
  

闽ICP备14008679号