当前位置:   article > 正文

小程序之实现列表切换与滚动加载

data-id="{{index}}" data-src="{{item}}

先看效果图:

180322_p8Hs_3472480.png181311_ms2Z_3472480.png

基于touchUI WX开发,片段来自码云 wallpaper 项目 

贴上代码:

  1. <template>
  2. <view class="collect">
  3. <view class="title-wire"></view>
  4. <!-- 横屏竖屏 -->
  5. <scroll-view class="recommend" scroll-y='true' style="height:{{height}}px" bindscrolltolower='lower'>
  6. <view style="height:50px"></view>
  7. <ui-fixed-view top="0" left="0" right="0">
  8. <view class="fixed-view-content">
  9. <view style="width:160px;margin:0 auto">
  10. <ui-tabs width=""
  11. tab-style="{{tabStyle}}"
  12. auto-width="{{false}}"
  13. index="{{ current4 }}"
  14. active-tab-style="{{activeTabStyle4}}"
  15. bindchange="handleChange4"
  16. probe="1"
  17. class="index-recommend">
  18. <ui-tab>
  19. <view class="item item-right">横屏壁纸</view>
  20. </ui-tab>
  21. <ui-tab>
  22. <view class="item item-left">
  23. 竖屏壁纸
  24. </view>
  25. </ui-tab>
  26. </ui-tabs>
  27. </view>
  28. </view>
  29. </ui-fixed-view>
  30. <view class="classify-cont">
  31. <!-- 横屏壁纸 -->
  32. <view class="classify-list classify-list-across"
  33. wx:if="{{current4 === 0}}"
  34. wx:for="{{getAllList}}"
  35. wx:key="index">
  36. <view class="classify-list-cont"
  37. data-id="{{index}}"
  38. data-url="{{item.imageUrl}}"
  39. data-text="横屏壁纸详情"
  40. bindtap="toDetails"
  41. >
  42. <image src="{{item.imageUrl}}" mode="aspectFill"/>
  43. </view>
  44. </view>
  45. <!-- 竖屏壁纸 -->
  46. <view class="classify-list classify-list-vertical"
  47. wx:if="{{current4 === 1}}"
  48. wx:for="{{getAllList}}"
  49. wx:key="index">
  50. <view class="classify-list-cont"
  51. data-id="{{index}}"
  52. data-url="{{item.imageUrl}}"
  53. data-text="竖屏壁纸详情"
  54. bindtap="toDetails"
  55. >
  56. <image src="{{item.imageUrl}}" mode="aspectFill"/>
  57. </view>
  58. </view>
  59. <view class="all-nothave" wx:if="{{isGetAllList}}">没有更多了</view>
  60. </view>
  61. </scroll-view>
  62. </view>
  63. </template>
  64. <script>
  65. export default {
  66. config: {
  67. navigationBarTitleText: '我的收藏'
  68. },
  69. data: {
  70. // 横屏竖屏切换
  71. current4: 0,
  72. tabStyle:{
  73. 'flex':'0 0 40px',
  74. 'padding':'10px 0px',
  75. 'width':'40px',
  76. 'margin': '0 20px',
  77. },
  78. activeTabStyle4:{
  79. "border-bottom": "2px solid #ff6630",
  80. },
  81. tabContents: [
  82. {subCategoryList: [
  83. {
  84. text: '1',
  85. imageUrl: "http://ipyd.paixian.com/wall/banner1.png",
  86. }, {
  87. text: '2',
  88. imageUrl: "http://ipyd.paixian.com/wall/banner2.png",
  89. }, {
  90. text: '3',
  91. imageUrl: "http://ipyd.paixian.com/wall/banner3.png",
  92. }
  93. ]},
  94. {subCategoryList: [
  95. {
  96. text: '1',
  97. imageUrl: "http://ipyd.paixian.com/wall/list/1.jpg",
  98. }, {
  99. text: '2',
  100. imageUrl: "http://ipyd.paixian.com/wall/list/2.jpg",
  101. }, {
  102. text: '3',
  103. imageUrl: "http://ipyd.paixian.com/wall/list/3.jpg",
  104. }, {
  105. text: '4',
  106. imageUrl: "http://ipyd.paixian.com/wall/list/4.jpg",
  107. }, {
  108. text: '5',
  109. imageUrl: "http://ipyd.paixian.com/wall/list/5.jpg",
  110. }, {
  111. text: '6',
  112. imageUrl: "http://ipyd.paixian.com/wall/list/6.jpg",
  113. }, {
  114. text: '7',
  115. imageUrl: "http://ipyd.paixian.com/wall/list/7.jpg",
  116. }, {
  117. text: '8',
  118. imageUrl: "http://ipyd.paixian.com/wall/list/8.jpg",
  119. }, {
  120. text: '9',
  121. imageUrl: "http://ipyd.paixian.com/wall/list/9.jpg",
  122. }, {
  123. text: '10',
  124. imageUrl: "http://ipyd.paixian.com/wall/list/10.jpg",
  125. }, {
  126. text: '11',
  127. imageUrl: "http://ipyd.paixian.com/wall/list/11.jpg",
  128. }, {
  129. text: '12',
  130. imageUrl: "http://ipyd.paixian.com/wall/list/12.jpg",
  131. }, {
  132. text: '13',
  133. imageUrl: "http://ipyd.paixian.com/wall/list/13.jpg",
  134. }, {
  135. text: '14',
  136. imageUrl: "http://ipyd.paixian.com/wall/list/14.jpg",
  137. }, {
  138. text: '15',
  139. imageUrl: "http://ipyd.paixian.com/wall/list/15.jpg",
  140. }, {
  141. text: '16',
  142. imageUrl: "http://ipyd.paixian.com/wall/list/16.jpg",
  143. }
  144. ]},
  145. ], // 横屏竖屏切换
  146. tabContentData: [], // 横屏竖屏后的列表
  147. // 滚动加载列表
  148. height: '',
  149. getAllList: [], // 模拟数据滚动加载,
  150. pageSize: 15, // 每页条数
  151. pageNo: 1, // 当前页数
  152. isGetAllList: false, // 是否显示没有更多了
  153. },
  154. //横屏竖屏切换
  155. handleChange4 (e) {
  156. let index = e.detail.index
  157. this.setData({
  158. current4:index,
  159. tabContentData:this.data.tabContents[index].subCategoryList
  160. })
  161. console.log(this.data.tabContentData)
  162. this.getAllList()
  163. },
  164. // 横屏竖屏切换后加载第一屏数据
  165. getAllList () {
  166. let _this = this
  167. _this.data.getAllList = [] // 切换后清除原有存储数据
  168. _this.data.isGetAllList = false
  169. wx.getSystemInfo({
  170. success: (res) => {
  171. let result = _this.data.tabContentData;
  172. for (let i = 0; i < _this.data.pageSize; i++) {
  173. if(_this.data.getAllList.length < _this.data.tabContentData.length){
  174. _this.data.getAllList.push(result[i])
  175. }
  176. }
  177. _this.setData({
  178. height: res.windowHeight,
  179. getAllList: _this.data.getAllList
  180. })
  181. // 判断展示没有更多了
  182. console.log(_this.data.getAllList.length,_this.data.tabContentData.length)
  183. if(_this.data.getAllList.length >= _this.data.tabContentData.length) {
  184. _this.setData({
  185. isGetAllList: true
  186. })
  187. return false
  188. } else {
  189. _this.setData({
  190. isGetAllList: false
  191. })
  192. }
  193. }
  194. })
  195. },
  196. onReady:function(){
  197. },
  198. // 因为是模拟数据返回的是全部,这里所以要做成分页的形式,如果是通过页数去请求接口再push就不需要这样子操作了
  199. lower() {
  200. if(this.data.getAllList.length < this.data.tabContentData.length){
  201. wx.showLoading({
  202. title: '加载中',
  203. icon: 'loading',
  204. })
  205. } else {
  206. this.setData({
  207. isGetAllList: true
  208. })
  209. }
  210. for (let i = 0; i < this.data.pageSize; i++) {
  211. let getAllListLength = this.data.getAllList.length
  212. if(this.data.getAllList.length < this.data.tabContentData.length){
  213. this.data.getAllList.push(this.data.tabContentData[getAllListLength++]);
  214. }
  215. }
  216. setTimeout(() => {
  217. this.setData({
  218. getAllList: this.data.getAllList
  219. });
  220. wx.hideLoading();
  221. }, 500)
  222. },
  223. //生命周期函数--监听页面加载
  224. onLoad: function (options) {
  225. this.setData({
  226. current4: 0,
  227. tabContentData:this.data.tabContents[0].subCategoryList
  228. })
  229. this.getAllList()
  230. },
  231. // 跳转到壁纸详情
  232. toDetails(e) {
  233. console.log(e.currentTarget.dataset)
  234. wx.navigateTo({
  235. url:
  236. "/pages/collectDetails/collectDetails?text="+ e.currentTarget.dataset.text +"&url=" + e.currentTarget.dataset.url, // 跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀
  237. success: function() {}, // 成功后的回调;
  238. fail: function() {}, // 失败后的回调;
  239. complete: function() {} // 结束后的回调(成功,失败都会执行)
  240. })
  241. },
  242. }
  243. </script>
  244. <style lang="less">
  245. .collect{
  246. .fixed-view-content {
  247. background-color: #fff;
  248. z-index: 99;
  249. }
  250. // 横屏竖屏切换
  251. .recommend{
  252. width: 100%;
  253. background: #fff;
  254. .classify-cont {
  255. display: flex;
  256. flex-wrap: wrap;
  257. justify-content: left;
  258. margin-top: -4px;
  259. padding-left: 15px;
  260. .classify-list {
  261. padding: 15px 15px 0px 0px;
  262. .classify-list-cont {
  263. border-radius: 4px;
  264. overflow: hidden;
  265. box-shadow: 0px 2px 4px 0px rgba(215,215,215,0.5);
  266. img{
  267. width: 100%;
  268. height: auto;
  269. }
  270. }
  271. }
  272. .classify-list-across{
  273. width: 50%;
  274. .classify-list-cont{
  275. height: 100px;
  276. }
  277. }
  278. .classify-list-vertical{
  279. width: 33.3%;
  280. .classify-list-cont {
  281. height: 150px;
  282. }
  283. }
  284. }
  285. }
  286. .all-nothave{
  287. width: 100%;
  288. text-align: center;
  289. color:#7c7c7c;
  290. padding: 20px 0;
  291. }
  292. }
  293. </style>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/321338
推荐阅读
相关标签