微信小程序实现分页下拉加载的实例代码
转眼间我们教你微信小程序系列已经有十节系列课程了,每天的工作压力繁重,小女子也不知道自己还能坚持这样的系列教程多久。只希望每篇教程真的对大家有帮助。这节课我们要介绍的就是如何实现分页的下拉加载,我们先来看效果图
<view class="copyright"> <view class="copyright_item">CopyRight:All Right Reserved</view> <view class="copyright_item">原创作者:51小程序</view> <view class="copyright_item">微信小程序开发者社区</view> <view class="copyright_item">HTML51.COM</view> <view class="copyright_item"><image class="img" src="../copyright/image/logo.png"/></view> <view class="goto_counter"><button type="default" bindtap="goto_counter">点击进入下拉加载演示页面</button></view> </view>
主要的页面结果如下:
1.index.wxml
<view class="search"> <view class="search-bar"> <view class="search-wrap"> <icon type="search" size="16" class="icon-search" /> <input type="text" placeholder="请输入搜索内容" class="search-input" name="searchKeyword" bindinput="bindKeywordInput" value="{{searchKeyword}}" /> </view> <view class="search-cancel" bindtap="keywordSearch">搜索</view> </view> <view class="search-result"> <scroll-view scroll-y="true" bindscrolltolower="searchScrollLower"> <view class="result-item" wx:for="{{searchSongList}}" wx:key="unique" data-data="{{item}}" > <view class="icon{{item.isonly=='0' ? ' nocopyright' : ''}}"></view> <text class="title">{{item.songname}}</text> <view class="subtitle"> <text wx:for="{{item.singer}}" wx:key="unique">{{item.name}}</text> </view> </view> <view class="loading" hidden="{{!searchLoading}}">正在载入更多...</view> <view class="loading complete" hidden="{{!searchLoadingComplete}}">已加载全部</view> </scroll-view> </view> </view>
2.index.wxss
page{ display: flex; flex-direction: column; height: 100%; } /*搜索*/ .search{ flex: auto; display: flex; flex-direction: column; background: #fff; } .search-bar{ flex: none; display: flex; align-items: center; justify-content: space-between; padding: 20rpx; background: #f4f4f4; } .search-wrap{ position: relative; flex: auto; display: flex; align-items: center; height: 80rpx; padding: 0 20rpx; background: #fff; border-radius: 6rpx; } .search-wrap .icon-search{ margin-right: 10rpx; } .search-wrap .search-input{ flex: auto; font-size: 28rpx; } .search-cancel{ padding: 0 20rpx; font-size: 28rpx; } /*搜索结果*/ .search-result{ flex: auto; position: relative; } .search-result scroll-view{ position: absolute; bottom: 0; left: 0; right: 0; top: 0; } .result-item{ position: relative; display: flex; flex-direction: column; padding: 20rpx 0 20rpx 110rpx; overflow: hidden; border-bottom: 2rpx solid #e5e5e5; } .result-item .media{ position: absolute; left: 16rpx; top: 16rpx; width: 80rpx; height: 80rpx; border-radius: 999rpx; } .result-item .title, .result-item .subtitle{ overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 36rpx; } .result-item .title{ margin-bottom: 4rpx; color: #000; } .result-item .subtitle{ color: #808080; font-size: 24rpx; } .result-item:first-child .subtitle text{ margin-right: 20rpx; } .result-item:not(:first-child) .subtitle text:not(:first-child):before{ content: '/'; margin: 0 8rpx; } .loading{ padding: 10rpx; text-align: center; } .loading:before{ display: inline-block; margin-right: 5rpx; vertical-align: middle; content: ''; width: 40rpx; height: 40rpx; background: url(../index/images/icon-loading.png) no-repeat; background-size: contain; animation: rotate 1s linear infinite; } .loading.complete:before{ display: none; }
3.index.js
var util = require('../../utils/util.js') Page({ data: { searchKeyword: '', //需要搜索的字符 searchSongList: [], //放置返回数据的数组 isFromSearch: true, // 用于判断searchSongList数组是不是空数组,默认true,空的数组 searchPageNum: 1, // 设置加载的第几次,默认是第一次 callbackcount: 15, //返回数据的个数 searchLoading: false, //"上拉加载"的变量,默认false,隐藏 searchLoadingComplete: false //“没有数据”的变量,默认false,隐藏 }, //输入框事件,每输入一个字符,就会触发一次 bindKeywordInput: function(e){ console.log("输入框事件") this.setData({ searchKeyword: e.detail.value }) }, //搜索,访问网络 fetchSearchList: function(){ let that = this; let searchKeyword = that.data.searchKeyword,//输入框字符串作为参数 searchPageNum = that.data.searchPageNum,//把第几次加载次数作为参数 callbackcount =that.data.callbackcount; //返回数据的个数 //访问网络 util.getSearchMusic(searchKeyword, searchPageNum,callbackcount, function(data){ console.log(data) //判断是否有数据,有则取数据 if(data.data.song.curnum != 0){ let searchList = []; //如果isFromSearch是true从data中取出数据,否则先从原来的数据继续添加 that.data.isFromSearch ? searchList=data.data.song.list : searchList=that.data.searchSongList.concat(data.data.song.list) that.setData({ searchSongList: searchList, //获取数据数组 zhida: data.data.zhida, //存放歌手属性的对象 searchLoading: true //把"上拉加载"的变量设为false,显示 }); //没有数据了,把“没有数据”显示,把“上拉加载”隐藏 }else{ that.setData({ searchLoadingComplete: true, //把“没有数据”设为true,显示 searchLoading: false //把"上拉加载"的变量设为false,隐藏 }); } }) }, //点击搜索按钮,触发事件 keywordSearch: function(e){ this.setData({ searchPageNum: 1, //第一次加载,设置1 searchSongList:[], //放置返回数据的数组,设为空 isFromSearch: true, //第一次加载,设置true searchLoading: true, //把"上拉加载"的变量设为true,显示 searchLoadingComplete:false //把“没有数据”设为false,隐藏 }) this.fetchSearchList(); }, //滚动到底部触发事件 searchScrollLower: function(){ let that = this; if(that.data.searchLoading && !that.data.searchLoadingComplete){ that.setData({ searchPageNum: that.data.searchPageNum+1, //每次触发上拉事件,把searchPageNum+1 isFromSearch: false //触发到上拉事件,把isFromSearch设为为false }); that.fetchSearchList(); } } })
以上是微信小程序实现分页下拉加载的实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

小程序开发中的PHP权限管理与用户角色设定随着小程序的普及和应用范围的扩大,用户对于小程序的功能和安全性提出了更高的要求,其中权限管理和用户角色设定是保证小程序安全性的重要一环。在小程序中使用PHP进行权限管理和用户角色设定能够有效地保护用户的数据和隐私,下面将介绍如何实现这一功能。一、权限管理的实现权限管理是指根据用户的身份和角色,授予不同的操作权限。在小

PHP在小程序开发中的页面跳转与路由管理随着小程序的快速发展,越来越多的开发者开始将PHP与小程序开发相结合。在小程序开发中,页面跳转和路由管理是非常重要的一部分,它能够帮助开发者实现页面之间的切换和导航操作。PHP作为一种常用的服务器端编程语言,可以很好地与小程序进行交互和数据传递,下面我们来详细了解一下PHP在小程序中的页面跳转与路由管理。一、页面跳转基

如何在uni-app中实现小程序开发和发布随着移动互联网的发展,小程序成为了移动应用开发的一个重要方向。而uni-app作为一个跨平台的开发框架,可以同时支持多个小程序平台的开发,如微信、支付宝、百度等。下面将详细介绍如何使用uni-app开发和发布小程序,并提供一些具体的代码示例。一、小程序开发前准备在开始使用uni-app开发小程序之前,需要做一些准备工

小程序开发中的PHP安全防护与攻击防范随着移动互联网的迅猛发展,小程序成为了人们生活中重要的一部分。而PHP作为一种强大而灵活的后端开发语言,也被广泛应用于小程序的开发中。然而,安全问题一直是程序开发中需要重视的方面。本文将重点介绍小程序开发中PHP的安全防护与攻击防范,同时提供一些代码示例。XSS(跨站脚本攻击)防范XSS攻击是指黑客通过向网页注入恶意脚本

小程序开发中的PHP数据缓存与缓存策略随着小程序的快速发展,更多的开发者开始关注如何提高小程序的性能和响应速度。其中一个重要的优化手段就是使用数据缓存来减少对数据库和外部接口的频繁访问。而在PHP中,我们可以利用各种缓存策略来实现数据缓存。本文将介绍PHP中的数据缓存原理,并提供几个常见的缓存策略的示例代码。一、数据缓存原理数据缓存是指将数据存放在内存中,以

今天我们来学习一下微信小程序中PHP开发的下拉菜单实现方法。微信小程序是一种轻量级的应用程序,用户可以在微信里直接使用,而且不需要下载安装,非常方便。而PHP是一种非常流行的后端编程语言,也是与微信小程序配合很好的一种语言。下面我们就来看看如何在微信小程序中使用PHP开发下拉菜单。首先,我们需要准备好开发环境,包括PHP、微信小程序开发工具和服务器。然后我们

小程序开发中的PHP页面动画效果与交互设计导语:小程序是一种在移动设备上运行的应用程序,能够提供类似原生应用的体验。而在小程序开发中,PHP作为一种常用的后端语言,可以为小程序页面增添动画效果与交互设计。本文将介绍一些常用的PHP页面动画效果与交互设计,并附上代码示例。一、CSS3动画CSS3提供了丰富的属性和方法,用于实现各种动画效果。而在小

UniApp实现字节跳动小程序的开发与上线流程解析字节跳动小程序作为一种新兴的移动应用开发方式,正逐渐在业界流行起来。在开发字节跳动小程序之前,我们需要了解如何使用UniApp来实现开发和上线的流程。一、UniApp简介UniApp是一套基于Vue.js开发的以HTML5、App、小程序为多端统一开发的框架,通过编写一套代码,可以同时在多个平台上运行,包括字
