Skip to content

Commit e48d0e8

Browse files
committed
修改
1 parent c946210 commit e48d0e8

4 files changed

Lines changed: 105 additions & 51 deletions

File tree

demo/App.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
html,body{margin: 0; padding: 0;}
22
li{font-size: 20px; width: 100%;list-style: none;}
33
img{width: 100%;}
4-
div, .test-ul, p{margin: 0; padding: 0;}
4+
div, .test-ul, p{margin: 0; padding: 0;}
5+
.block{position: relative; top: 100px; height: 500px; overflow-y: scroll; -webkit-overflow-scrolling: touch;}

demo/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class App extends Component{
4646
const {data, hasMore} = this.state
4747
return (
4848
<div>
49-
<ReactPullLoad onRefresh={this.refresh.bind(this)} onLoadMore={this.loadMore.bind(this)} hasMore={hasMore}>
49+
<ReactPullLoad downEnough={150} onRefresh={this.refresh.bind(this)} onLoadMore={this.loadMore.bind(this)} hasMore={hasMore}>
5050
<ul className="test-ul">
5151
{
5252
data.map( (str, index )=>{

demo/App1.jsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import React, { Component, PropTypes } from 'react'
2+
import { render } from 'react-dom'
3+
import ReactPullLoad from 'ReactPullLoad'
4+
import './App.css'
5+
6+
const cData = [
7+
"http://img1.gtimg.com/15/1580/158031/15803178_1200x1000_0.jpg",
8+
"http://img1.gtimg.com/15/1580/158031/15803179_1200x1000_0.jpg",
9+
"http://img1.gtimg.com/15/1580/158031/15803181_1200x1000_0.jpg",
10+
"http://img1.gtimg.com/15/1580/158031/15803182_1200x1000_0.jpg",
11+
"http://img1.gtimg.com/15/1580/158031/15803183_1200x1000_0.jpg",
12+
"http://img1.gtimg.com/15/1580/158031/15803184_1200x1000_0.jpg",
13+
"http://img1.gtimg.com/15/1580/158031/15803186_1200x1000_0.jpg"
14+
]
15+
16+
export class App extends Component{
17+
constructor(){
18+
super();
19+
this.refreshResolve = null //用于保存刷新或者加载更多的resolve函数
20+
this.loadMore = this.loadMore.bind(this);
21+
this.refresh = this.refresh.bind(this);
22+
this.state ={
23+
hasMore: true,
24+
data: cData
25+
}
26+
}
27+
//刷新
28+
refresh(resolve, reject) {
29+
setTimeout(()=>{
30+
this.setState({
31+
data: cData
32+
});
33+
resolve();
34+
},3000)
35+
}
36+
//加载更多
37+
loadMore(resolve){
38+
setTimeout(()=>{
39+
this.setState({
40+
data: [...this.state.data, cData[0], cData[0]]
41+
});
42+
resolve();
43+
},3000)
44+
}
45+
render(){
46+
const {data, hasMore} = this.state
47+
return (
48+
<div>
49+
<ReactPullLoad className="block" style={{top:"200px"}} isBlockContainer={true} onRefresh={this.refresh.bind(this)} onLoadMore={this.loadMore.bind(this)} hasMore={hasMore}>
50+
<ul className="test-ul">
51+
{
52+
data.map( (str, index )=>{
53+
return <li key={index}><img src={str} alt=""/></li>
54+
})
55+
}
56+
</ul>
57+
</ReactPullLoad>
58+
</div>
59+
)
60+
}
61+
}
62+
63+
render(
64+
<App />,
65+
document.getElementById('root')
66+
)

src/ReactPullLoad.jsx

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export default class ReactPullLoad extends Component {
4040
this.onTouchStart = this.onTouchStart.bind(this)
4141
this.onTouchMove = this.onTouchMove.bind(this)
4242
this.onTouchEnd = this.onTouchEnd.bind(this)
43+
this.onPullDownMove = this.onPullDownMove.bind(this)
44+
this.onPullDownRefresh = this.onPullDownRefresh.bind(this)
45+
this.onPullUpMove = this.onPullUpMove.bind(this)
4346
this.container = null
4447
this.state = {
4548
loaderState: STATS.init,
@@ -48,18 +51,14 @@ export default class ReactPullLoad extends Component {
4851
}
4952

5053
componentDidMount() {
54+
const {isBlockContainer, offsetScrollTop, downEnough, distanceBottom} = this.props
5155
this.defaultConfig = {
52-
container: document.body,//findDOMNode(this),
53-
offsetScrollTop: this.props.offsetScrollTop || ReactPullLoad.offsetScrollTop,
54-
downEnough: this.props.downEnough || ReactPullLoad.downEnough,
55-
distanceBottom: this.props.distanceBottom || ReactPullLoad.distanceBottom,
56-
onPullDownMove: this.onPullDownMove.bind(this),
57-
onPullDownRefresh: this.onPullDownRefresh.bind(this),
58-
clearPullDownMove: this.clearPullDownMove.bind(this),
59-
onPullStart: this.onPullStart.bind(this),
60-
onPullUpMove: this.onPullUpMove.bind(this),
61-
onPullUpLoad: this.onPullUpLoad.bind(this),
56+
container: isBlockContainer ? findDOMNode(this) : document.body,
57+
offsetScrollTop: offsetScrollTop,
58+
downEnough: downEnough,
59+
distanceBottom: distanceBottom
6260
};
61+
console.info("downEnough = ", downEnough, this.defaultConfig.downEnough)
6362
addEvent(this.refs.container, "touchstart", this.onTouchStart)
6463
addEvent(this.refs.container, "touchmove", this.onTouchMove)
6564
addEvent(this.refs.container, "touchend", this.onTouchEnd)
@@ -114,7 +113,6 @@ export default class ReactPullLoad extends Component {
114113
})
115114
if (typeof this.props.onRefresh === "function") {
116115
this.props.onRefresh(() => {
117-
console.info("STATS.refreshed")
118116
this.setState({
119117
pullHeight: 0,
120118
loaderState: STATS.refreshed
@@ -129,16 +127,6 @@ export default class ReactPullLoad extends Component {
129127
}
130128
}
131129

132-
clearPullDownMove() {
133-
if (typeof this.props.clearPullDownMove === "function") {
134-
this.props.clearPullDownMove();
135-
}
136-
}
137-
onPullStart() {
138-
if (typeof this.props.onPullStart === "function") {
139-
this.props.onPullStart();
140-
}
141-
}
142130
onPullUpMove(data) {
143131
if(!this.canRefresh())return false;
144132
const { hasMore, onLoadMore} = this.props
@@ -148,14 +136,11 @@ export default class ReactPullLoad extends Component {
148136
loaderState: STATS.loading
149137
})
150138

151-
// console.info(this.state);
152139
onLoadMore(()=>{
153140
this.setState(endState)
154141
});
155142
}
156143
}
157-
onPullUpLoad() {
158-
}
159144

160145
onTouchStart(event) {
161146
console.info("onTouchStart")
@@ -167,28 +152,28 @@ export default class ReactPullLoad extends Component {
167152
onTouchMove(event) {
168153
let scrollTop = this.defaultConfig.container.scrollTop,
169154
scrollH = this.defaultConfig.container.scrollHeight,
170-
con_H = document.documentElement.clientHeight,//this.defaultConfig.container.clientHeight,
155+
conH = this.defaultConfig.container === document.body ? document.documentElement.clientHeight : this.defaultConfig.container.offsetHeight,
171156
targetEvent = event.changedTouches[0],
172157
curX = targetEvent.clientX,
173158
curY = targetEvent.clientY,
174159
diffX = curX - this.startX,
175160
diffY = curY - this.startY;
176-
// console.info(diffY, (scrollH - scrollTop - con_H))
161+
177162
//判断垂直移动距离是否大于5 && 横向移动距离小于纵向移动距离
178163
if (Math.abs(diffY) > 5 && Math.abs(diffY) > Math.abs(diffX)) {
179164
//滚动距离小于设定值 &&回调onPullDownMove 函数,并且回传位置值
180165
if (diffY > 5 && scrollTop < this.defaultConfig.offsetScrollTop) {
181166
//阻止执行浏览器默认动作
182167
event.preventDefault();
183-
this.defaultConfig.onPullDownMove([{
168+
this.onPullDownMove([{
184169
touchStartY: this.startY,
185170
touchMoveY: curY
186171
}]);
187172
} //滚动距离距离底部小于设定值
188-
else if (diffY < 0 && (scrollH - scrollTop - con_H) < this.defaultConfig.distanceBottom) {
173+
else if (diffY < 0 && (scrollH - scrollTop - conH) < this.defaultConfig.distanceBottom) {
189174
//阻止执行浏览器默认动作
190175
// event.preventDefault();
191-
this.defaultConfig.onPullUpMove([{
176+
this.onPullUpMove([{
192177
touchStartY: this.startY,
193178
touchMoveY: curY
194179
}]);
@@ -198,8 +183,6 @@ export default class ReactPullLoad extends Component {
198183

199184
onTouchEnd(event) {
200185
let scrollTop = this.defaultConfig.container.scrollTop,
201-
scrollH = this.defaultConfig.container.scrollHeight,
202-
con_H = document.documentElement.clientHeight,//this.defaultConfig.container.clientHeight,
203186
targetEvent = event.changedTouches[0],
204187
curX = targetEvent.clientX,
205188
curY = targetEvent.clientY,
@@ -210,26 +193,23 @@ export default class ReactPullLoad extends Component {
210193
if (Math.abs(diffY) > 5 && Math.abs(diffY) > Math.abs(diffX)) {
211194
if (diffY > 5 && scrollTop < this.defaultConfig.offsetScrollTop) {
212195
//回调onPullDownRefresh 函数,即满足刷新条件
213-
this.defaultConfig.onPullDownRefresh();
214-
}
215-
// else if(diffY < 0 && (scrollH - scrollTop - con_H) < this.defaultConfig.distanceBottom ){
216-
// //回调onPullUpLoad 函数,即满足刷新条件
217-
// this.defaultConfig.onPullUpLoad();
218-
// }
219-
else {
220-
//回调clearPullDownMove 函数,取消刷新动作
221-
this.defaultConfig.clearPullDownMove();
196+
this.onPullDownRefresh();
222197
}
223198
}
224199
}
225200

226201
render() {
227-
let {
202+
const {
228203
children,
229204
onRefresh,
230205
onLoadMore,
231206
hasMore,
232207
initializing,
208+
className,
209+
offsetScrollTop,
210+
downEnough,
211+
distanceBottom,
212+
isBlockContainer,
233213
...other
234214
} = this.props
235215

@@ -246,10 +226,10 @@ export default class ReactPullLoad extends Component {
246226
transform: `translate3d(0, ${symbolTop}px, 0)`
247227
} : null;
248228

249-
const boxClassName = 'tloader state-' + loaderState;
229+
const boxClassName = `${className} tloader state-${loaderState}`;
250230

251231
return (
252-
<div className={boxClassName} ref="container">
232+
<div {...other} className={boxClassName} ref="container">
253233
<div className="tloader-symbol" style={msgStyle2}>
254234
<p className="tloader-msg"><i></i></p>
255235
<p className="tloader-loading">
@@ -273,11 +253,18 @@ export default class ReactPullLoad extends Component {
273253
ReactPullLoad.propTypes = {
274254
onRefresh: PropTypes.func.isRequired,
275255
onLoadMore: PropTypes.func,
276-
hasMore: PropTypes.bool,
256+
hasMore: PropTypes.bool, //是否还有更多内容可加载
277257
offsetScrollTop: PropTypes.number,
278-
downEnough: PropTypes.number,
279-
distanceBottom: PropTypes.number
258+
downEnough: PropTypes.number, //下拉满足刷新的距离
259+
distanceBottom: PropTypes.number, //距离底部距离触发加载更多
260+
isBlockContainer: PropTypes.bool
280261
}
281-
ReactPullLoad.offsetScrollTop = 2 //与顶部的距离
282-
ReactPullLoad.downEnough = 100 //下拉满足刷新的距离
283-
ReactPullLoad.distanceBottom = 100 //距离底部距离触发加载更多
262+
//设置 props 默认值
263+
ReactPullLoad.defaultProps = {
264+
hasMore: true,
265+
offsetScrollTop: 1,
266+
downEnough: 100,
267+
distanceBottom: 100,
268+
isBlockContainer: false,
269+
className: ""
270+
};

0 commit comments

Comments
 (0)