从微信小程序到鸿蒙js开发【13】——list加载更多&回到顶部
鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?【课程入口】
目录:
1、list加载更多
如果在list中需要展示的数据非常多,那么一次性获取全部数据并显示,对于后端服务器和前段渲染的性能都是很大的负担,浪费资源且页面加载速度会很慢。
在网页端做分页普遍是用户点击“上一页”,“下一页”进行翻页,而移动端设备一般是在滑动到页面底端后加载下一页数据,并将数据接在列表底部。在list组件中,可以通过onscrollbottom属性绑定事件并处理。

视觉效果上来看数据是连续的,但其中已经触发了一次翻页。

list部分 hml视图层:
<list scrollbar="auto" scrolleffect="no" onscrollbottom="loadMore" id="list">
<block for="{{ comments }}">
<list-item>
<div>
<image src="/common/user.png"></image>
<div class="title">
<text style="color: #333333; font-size: 32px;">
{{ $item.user.username }}
</text>
<text style="color: #666666; font-size: 30px;">
{{ $item.date }}
</text>
</div>
<rating numstars="5" rating="{{ $item.star }}" indicator="true"></rating>
</div>
<text class="content">
{{ $item.content }}
</text>
</list-item>
</block>
</list>
css渲染层:
list {
width: 100%;
height: 1400px;
}
list-item {
width: 100%;
border-bottom: 1px solid #bbbbbb;
background-color: #fdfdfd;
margin-bottom: 10px;
display: flex;
flex-direction: column;
padding: 10px 0 10px 0;
}
list-item image {
width: 60px;
height: 60px;
border-radius: 30px;
margin-left: 20px;
margin-top: 20px;
object-fit: contain;
}
.title {
margin-left: 20px;
height: 100px;
display: flex;
flex-direction: column;
width: 450px;
}
.title>text {
height: 50px;
line-height: 50px;
}
rating {
width: 150px;
height: 50px;
}
.content {
margin: 10px 20px 10px 20px;
font-size: 30px;
color: #333333;
}
js逻辑层:
import fetch from '@system.fetch';
import prompt from '@system.prompt';
export default {
data: {
......
comments: [],
page: 1,
maxPage: 1
},
onInit() {
this.listComments();
},
// list触底加载下一页数据
loadMore() {
if (this.page < this.maxPage) {
this.page++;
this.listComments();
} else {
prompt.showToast({
message: "已经到底啦",
duration: 3000
})
}
},
// 分页请求评论
listComments() {
fetch.fetch({
url: this.url + "/list?goodsId=" + this.id + "&pageNo=" + this.page,
responseType: "json",
success: res => {
console.info(res.data);
let data = JSON.parse(res.data);
if (0 != data.code) {
prompt.showToast({
message: "服务错误",
duration: 3000
})
} else {
data.data.list.forEach(ele => {
this.comments.push(ele);
});
this.page = data.data.page;
this.maxPage = data.data.maxPage;
}
}
})
}
在服务器端,每次请求返回十条数据,以及当前页数、总页数。

2、list回到顶部
查看了一部分评论后,如果想要回到第一条评论的位置,需有一个“回到顶部”按钮,点击后列表自动滚动到最顶部。
在官方文档list组件中,未提到如何实现这样的功能。但在js中获取组件实例后,有这么几个API可供调用:

猜测是可以使list滚动,我们使用scrollTop(),使列表滚动到最顶端。
this.$element("list").scrollTop();

这样是不起作用的,虽然源代码注释的意思似乎是smooth默认为false。

smooth为false的效果,可以回到顶部,但比较生硬。
this.$element("list").scrollTop({
smooth: false
});

smooth: true的效果,还是不错的。

按钮使用type="circle",便可指定icon,实现图标按钮。
hml视图层:
<button onclick="toTop" type="circle" icon="/common/totop.png"></button>
css渲染层:
button {
position: fixed;
right: 20px;
bottom: 20px;
background-color: #eeeeee;
}
js逻辑层:
toTop() {
this.$element("list").scrollTop({
smooth: true
});
},
作者:Chris.
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com
从微信小程序到鸿蒙js开发【13】——list加载更多&回到顶部的更多相关文章
- 从微信小程序到鸿蒙js开发【11】——页面路由
目录: 1.router.push()&wx.navigateTo() 2.router.replace()&wx.redirectTo() 3.router.back()&w ...
- 从微信小程序到鸿蒙js开发【12】——storage缓存&自动登录
鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 正文: 在应用开发时,我们常需要将一些数据缓存到本地,以提升用户体验.比如在一个电商的app中,如果希望用户登录成功后,下次打 ...
- 从微信小程序到鸿蒙js开发【15】——JS调用Java
鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 目录:1.新建一个Service Ability2.完善代码逻辑3.JS端远程调用4.<从微信小 ...
- 微信小程序实现上拉和下拉加载更多
在上一篇文章中,我们知道了使用 scroll-view 可以实现上拉加载更多,但是由于 scroll-view 的限制,它无法实现下拉加载更多,这篇文章我们使用 view 组件来实现 上拉和下拉加载更 ...
- 【微信小程序】转载:微信小程序实战篇-下拉刷新与加载更多
下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 当然,你可以直接在全局变量app.json的window里面配置上面这个属性,这样整个项目都允许下 ...
- 从微信小程序到鸿蒙js开发【04】——list组件
目录: 1.可滚动区域 2.list + list-item 3.list + list-item-group + list-item 1.可滚动区域 在许多场景中,页面会有一块区域是可滚动的,比如这 ...
- 从微信小程序到鸿蒙js开发【06】——swiper&animator&marquee
目录: 1.swiper轮播图 2.image-animator幻灯片 3.marquee跑马灯 4.nginx动静分离 1.swiper轮播图 微信小程序的swiper组件中只能放置swiper-i ...
- 从微信小程序到鸿蒙js开发【08】——表单组件&注册登录模块
目录: 1.登录模块 2.注册模块 3.系列文章导读 牛年将至,祝大家行行无bug,页页so easy- 在微信小程序中,提供了form组件,可以将input.picker.slider.button ...
- 微信小程序之下拉刷新,上拉加载更多
近日开发微信小程序,发现上拉加载更多没有友好的API,而下拉刷新很nice,所以本人按照API,很简单的写了一个示例,希望对大家有帮助,本人用的是iview-webapp 小程序UI框架. 1. 首 ...
随机推荐
- centos7 快速搭建redis集群环境
本文主要是记录一下快速搭建redis集群环境的方式. 环境简介:centos 7 + redis-3.2.4 本次用两个服务6个节点来搭建:192.168.116.120 和 192.168.1 ...
- Spring5源码,@Autowired
一.@Autowired所具有的功能 二.在Spring中如何使用@Autowired 三.@Autowired注解背后的工作原理 一.@Autowired所具有的功能 @Autowired是一个用来 ...
- linux 用户、用户组及相关命令(useradd 、passwd、userdel 、groupadd 、groupdel、usermod 、gpasswd 、 id、su)
linux是一个多用户系统,用于权限管理(权限最小化); 相关命令: 7 8 9 10 11 12 13 14 15 useradd passwd userdel groupadd groupdel ...
- UVALive 7276 Wooden Signs
详细题目见:http://7xjob4.com1.z0.glb.clouddn.com/0f10204481da21e62f8c145939e5828e 思路:记dp[i][j]表示第i个木板尾部在j ...
- Codeforces Round #550 (Div. 3) D. Equalize Them All (贪心,模拟)
题意:有一组数,可以选择某个数\(a_i\)相邻的一个数\(a_j\),然后可以让\(a_i\)加上或者减去\(|a_i-a_j|\),问最少操作多少次使得数组中所有数相同. 题解:不难发现,每次操作 ...
- Keepalived+LVS实现LNMP网站的高可用部署
Keepalived+LVS实现LNMP网站的高可用部署 项目需求 当我们访问某个网站的时候可以在浏览器中输入IP或者域名链接到Web Server进行访问,如果这个Web Server挂了, ...
- python之字符串strip、rstrip、lstrip的方法
1.描述 strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列 rstrip():用于移除字符串右边指定的字符(默认为空格或换行符)或字符序列 lstrip():用于移除字符串 ...
- docker的底层-隔离的核心
在了解底层原理之前: 说几个名词: 解耦状态: 所有东西都没有重复,任何东西都没有公用的地方. 半解耦状态:有部分共同的一起用,其他的独立 完全解耦状态: 就是各自都是独立没有重复. kvm:完全解耦 ...
- codeforces 1013B 【思维+并查集建边】
题目链接:戳这里 转自:参考博客 题意:给一个n*m的矩阵,放入q个点,这q个点之间的关系是,若已知这样三个点(x1,y1),(x2,y1),(x1,y2),可以在(x2,y2)处生成一个新的点,对于 ...
- 一个操作系统的实现sudo mount -o loop pm.img /mnt/floppy mount point /mnt/floppy does not exist losetup device is busy
部分参考:https://blog.csdn.net/u012323667/article/details/79266623 一. sudo mount -o loop pm.img /mnt/flo ...