标签:
经过几天的反复折腾,总算做出一个体验还不错的列表页了,主要支持了下拉刷新,上拉加载两个功能。
一开始直接采用了react-iscroll插件,它是基于iscroll插件开发的组件。但是开发过程中,发现它内部封装的行为非常固化,限制了我对iscroll的控制能力,因此我转而直接基于iscroll插件实现。
网上也有一些基于浏览器原生滚动条实现的方案,找不到特别好的博客说明,而iscroll是基于Js模拟的滚动条(滚动条也是一个div哦),其兼容性更好,所以还是选择iscroll吧。
在讲解实现之前,可以先体验一下app整体效果。如果使用桌面浏览器访问,必须进入开发者模式,启动手机仿真,并使用鼠标左键触发滑动,否则无法达到真机效果(点我进入)!建议还是扫描二维码直接在手机浏览器中体验,二维码如下:

点击这里下载源码,之后一起看一下实现中需要注意的事项和思路。
本篇实现了MsgListPage这个组件,支持消息列表的滚动查看,下拉刷新,上拉加载功能。
这里使用了开源的iscroll5实现滚动功能,它对iscroll4重构并修复若干bug,是目前主流版本。网上鲜有iscroll5实现下拉刷新,上拉加载功能的好例子,提供的仅是一些思路,绝大多数实现都是修改iscroll5源码,并不完美。我这次的实现不需要修改iscroll5源码,其实通过巧妙的设计是可以完美的实现这些特效的。
代码如下:
import React from "react";
import {Link} from "react-router";
import $ from "jquery";
import style from "./MsgListPage.css";
import iScroll from "iscroll/build/iscroll-probe"; // 只有这个库支持onScroll,从而支持bounce阶段的事件捕捉
export default class MsgListPage extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
items: [],
pullDownStatus: 3,
pullUpStatus: 0,
};
this.page = 1;
this.itemsChanged = false;
this.pullDownTips = {
// 下拉状态
0: ‘下拉发起刷新‘,
1: ‘继续下拉刷新‘,
2: ‘松手即可刷新‘,
3: ‘正在刷新‘,
4: ‘刷新成功‘,
};
this.pullUpTips = {
// 上拉状态
0: ‘上拉发起加载‘,
1: ‘松手即可加载‘,
2: ‘正在加载‘,
3: ‘加载成功‘,
};
this.isTouching = false;
this.onItemClicked = this.onItemClicked.bind(this);
this.onScroll = this.onScroll.bind(this);
this.onScrollEnd = this.onScrollEnd.bind(this);
this.onTouchStart = this.onTouchStart.bind(this);
this.onTouchEnd = this.onTouchEnd.bind(this);
}
componentDidMount() {
const options = {
// 默认iscroll会拦截元素的默认事件处理函数,我们需要响应onClick,因此要配置
preventDefault: false,
// 禁止缩放
zoom: false,
// 支持鼠标事件,因为我开发是PC鼠标模拟的
mouseWheel: true,
// 滚动事件的探测灵敏度,1-3,越高越灵敏,兼容性越好,性能越差
probeType: 3,
// 拖拽超过上下界后出现弹射动画效果,用于实现下拉/上拉刷新
bounce: true,
// 展示滚动条
scrollbars: true,
};
this.iScrollInstance = new iScroll(`#${style.ListOutsite}`, options);
this.iScrollInstance.on(‘scroll‘, this.onScroll);
this.iScrollInstance.on(‘scrollEnd‘, this.onScrollEnd);
this.fetchItems(true);
}
fetchItems(isRefresh) {
if (isRefresh) {
this.page = 1;
}
$.ajax({
url: ‘/msg-list‘,
data: {page: this.page},
type: ‘GET‘,
dataType: ‘json‘,
success: (response) => {
if (isRefresh) { // 刷新操作
if (this.state.pullDownStatus == 3) {
this.setState({
pullDownStatus: 4,
items: response.data.items
});
this.iScrollInstance.scrollTo(0, -1 * $(this.refs.PullDown).height(), 500);
}
} else { // 加载操作
if (this.state.pullUpStatus == 2) {
this.setState({
pullUpStatus: 0,
items: this.state.items.concat(response.data.items)
});
}
}
++this.page;
console.log(`fetchItems=effected isRefresh=${isRefresh}`);
}
});
}
/**
* 点击跳转详情页
*/
onItemClicked(ev) {
// 获取对应的DOM节点, 转换成jquery对象
let item = $(ev.target);
// 操作router实现页面切换
this.context.router.push(item.attr(‘to‘));
this.context.router.goForward();
}
onTouchStart(ev) {
this.isTouching = true;
}
onTouchEnd(ev) {
this.isTouching = false;
}
onPullDown() {
// 手势
if (this.isTouching) {
if (this.iScrollInstance.y > 5) {
this.state.pullDownStatus != 2 && this.setState({pullDownStatus: 2});
} else {
this.state.pullDownStatus != 1 && this.setState({pullDownStatus: 1});
}
}
}
onPullUp() {
// 手势
if (this.isTouching) {
if (this.iScrollInstance.y <= this.iScrollInstance.maxScrollY - 5) {
this.state.pullUpStatus != 1 && this.setState({pullUpStatus: 1});
} else {
this.state.pullUpStatus != 0 && this.setState({pullUpStatus: 0});
}
}
}
onScroll() {
let pullDown = $(this.refs.PullDown);
// 上拉区域
if (this.iScrollInstance.y > -1 * pullDown.height()) {
this.onPullDown();
} else {
this.state.pullDownStatus != 0 && this.setState({pullDownStatus: 0});
}
// 下拉区域
if (this.iScrollInstance.y <= this.iScrollInstance.maxScrollY + 5) {
this.onPullUp();
}
}
onScrollEnd() {
console.log("onScrollEnd" + this.state.pullDownStatus);
let pullDown = $(this.refs.PullDown);
// 滑动结束后,停在刷新区域
if (this.iScrollInstance.y > -1 * pullDown.height()) {
if (this.state.pullDownStatus <= 1) { // 没有发起刷新,那么弹回去
this.iScrollInstance.scrollTo(0, -1 * $(this.refs.PullDown).height(), 200);
} else if (this.state.pullDownStatus == 2) { // 发起了刷新,那么更新状态
this.setState({pullDownStatus: 3});
this.fetchItems(true);
}
}
// 滑动结束后,停在加载区域
if (this.iScrollInstance.y <= this.iScrollInstance.maxScrollY) {
if (this.state.pullUpStatus == 1) { // 发起了加载,那么更新状态
this.setState({pullUpStatus: 2});
this.fetchItems(false);
}
}
}
shouldComponentUpdate(nextProps, nextState) {
// 列表发生了变化, 那么应该在componentDidUpdate时调用iscroll进行refresh
this.itemsChanged = nextState.items !== this.state.items;
return true;
}
componentDidUpdate() {
// 仅当列表发生了变更,才调用iscroll的refresh重新计算滚动条信息
if (this.itemsChanged) {
this.iScrollInstance.refresh();
}
return true;
}
render() {
let lis = [];
this.state.items.forEach((item, index) => {
lis.push(
<li key={index} to={`/msg-detail-page/${index}`} onClick={this.onItemClicked}>
{item.title}{index}
</li>
);
})
// 外层容器要固定高度,才能使用滚动条
return (
<div id={style.ScrollContainer}>
<div id={style.ListOutsite} style={{height: window.innerHeight}}
onTouchStart={this.onTouchStart} onTouchEnd={this.onTouchEnd}>
<ul id={style.ListInside}>
<p ref="PullDown" id={style.PullDown}>{this.pullDownTips[this.state.pullDownStatus]}</p>
{lis}
<p ref="PullUp" id={style.PullUp}>{this.pullUpTips[this.state.pullUpStatus]}</p>
</ul>
</div>
</div>
);
}
}
MsgListPage.contextTypes = {
router: () => { React.PropTypes.object.isRequired }
};
必须注意,所有的网络请求都是模拟的,并没有动态的后端计算。
本文实现了非常有意思的动画效果,也非常实用。
另外,第3个组件『留言提交页』因为精力原因,不打算继续写完了。
当前访问路径如果是:列表页 -> 详情页 -> 返回列表页,会发现列表页内容重新刷新了,滚动条也没有停留在原先的位置上。这是因为每次路由切换,都是重新分配一个component对象进行重新渲染,所以状态没有保存,我当然可以在跳转详情页之前把列表页的状态保存到一个全局变量里或者localStorage里,但是这毕竟比较麻烦。
为了实现状态保存,redux就是在做类似的框架级支持,所以我可能接下来真的要学学redux了,学无止境,太可怕!
react + iscroll5 实现完美 下拉刷新,上拉加载
标签:
原文地址:http://www.cnblogs.com/qq120848369/p/5920420.html