鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?【课程入口】

目录:

1、list加载更多

2、list回到顶部

3、《从微信小程序到鸿蒙js开发》系列文章合集

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加载更多&回到顶部的更多相关文章

  1. 从微信小程序到鸿蒙js开发【11】——页面路由

    目录: 1.router.push()&wx.navigateTo() 2.router.replace()&wx.redirectTo() 3.router.back()&w ...

  2. 从微信小程序到鸿蒙js开发【12】——storage缓存&自动登录

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 正文: 在应用开发时,我们常需要将一些数据缓存到本地,以提升用户体验.比如在一个电商的app中,如果希望用户登录成功后,下次打 ...

  3. 从微信小程序到鸿蒙js开发【15】——JS调用Java

    鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] 目录:1.新建一个Service Ability2.完善代码逻辑3.JS端远程调用4.<从微信小 ...

  4. 微信小程序实现上拉和下拉加载更多

    在上一篇文章中,我们知道了使用 scroll-view 可以实现上拉加载更多,但是由于 scroll-view 的限制,它无法实现下拉加载更多,这篇文章我们使用 view 组件来实现 上拉和下拉加载更 ...

  5. 【微信小程序】转载:微信小程序实战篇-下拉刷新与加载更多

    下拉刷新 实现下拉刷新目前能想到的有两种方式 1. 调用系统的API,系统有提供下拉刷新的API接口 当然,你可以直接在全局变量app.json的window里面配置上面这个属性,这样整个项目都允许下 ...

  6. 从微信小程序到鸿蒙js开发【04】——list组件

    目录: 1.可滚动区域 2.list + list-item 3.list + list-item-group + list-item 1.可滚动区域 在许多场景中,页面会有一块区域是可滚动的,比如这 ...

  7. 从微信小程序到鸿蒙js开发【06】——swiper&animator&marquee

    目录: 1.swiper轮播图 2.image-animator幻灯片 3.marquee跑马灯 4.nginx动静分离 1.swiper轮播图 微信小程序的swiper组件中只能放置swiper-i ...

  8. 从微信小程序到鸿蒙js开发【08】——表单组件&注册登录模块

    目录: 1.登录模块 2.注册模块 3.系列文章导读 牛年将至,祝大家行行无bug,页页so easy- 在微信小程序中,提供了form组件,可以将input.picker.slider.button ...

  9. 微信小程序之下拉刷新,上拉加载更多

    近日开发微信小程序,发现上拉加载更多没有友好的API,而下拉刷新很nice,所以本人按照API,很简单的写了一个示例,希望对大家有帮助,本人用的是iview-webapp  小程序UI框架. 1. 首 ...

随机推荐

  1. SparkStreaming算子操作,Output操作

    SparkStreaming练习之StreamingTest,UpdateStateByKey,WindowOperator 一.SparkStreaming算子操作 1.1 foreachRDD 1 ...

  2. charles(2)MAC Charles关闭后无法上网

    前言 charles关闭后,发现网页突然打开了,那大概率是设置了代理,但明明已经关闭了charles,这是由于mac网络偏好设置中,使用的是手动代理,将其改为自动即可 解决方法 1 打开网络偏好设置, ...

  3. C++多元组tuple使用方法?你熟悉吗?快来看看吧

  4. Kubernetes-5-2:Harbor仓库的几种高可用方案与搭建

    高可用Harbor搭建 思路及介绍 Harbor官方有推出主从架构和双主架构来实现Harbor的高可用及数据备份.   一.主从架构:  说白了,就是往一台Harbor仓库中push镜像,然后再通过这 ...

  5. Codeforces Round #628 (Div. 2) A. EhAb AnD gCd(LCM & GCD)

    题意: GCD(a,b) + LCM(a,b) = n,已知 n ,求 a,b. 思路: 设 gcd(a, b) = k, a = xk, b = yk , k + ab / k = n xy = n ...

  6. priority_queue()大根堆和小根堆(二叉堆)

    #include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...

  7. hdu5726 GCD(gcd +二分+rmq)

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  8. Codeforces Round #669 (Div. 2) B. Big Vova (枚举)

    题意:有一个长度为\(n\)的序列,你需要对其重新排序,构造一个新数组\(c\),\(c_{i}=gcd(a_{1},...,a{i})\)并且使得\(c\)的字典序最小. 题解:直接跑\(n\)次, ...

  9. 9.PowerShell DSC之Pull

    前言 一般生产环境都使用Pull模式 配置Pull Server 配置Pull Server需要安装两个WindowsFeture:IIS.windows DSC,这两都可以通过UI界面化引导安装,也 ...

  10. Win7下安装IIS

    安装IIS 1.控制面板 --> 程序 --> 卸载程序,进入"程序与功能". 2.进入"打开或关闭Window功能". 3.找到"Int ...