@@ -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 {
273253ReactPullLoad . 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