-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli h5): 支持onPageScroll和onReachBottom api
- Loading branch information
Showing
7 changed files
with
219 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './pageScroll' | ||
export * from './reachBottom' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createCallbackManager, createScroller } from '../utils' | ||
|
||
export const onPageScroll = (opt) => { | ||
const callbackManager = createCallbackManager() | ||
const scroller = createScroller(opt.ctx) | ||
const onScroll = () => { | ||
callbackManager.trigger({ | ||
scrollTop: scroller.getPos() | ||
}) | ||
} | ||
|
||
callbackManager.add(opt) | ||
scroller.listen(onScroll) | ||
|
||
return () => { | ||
callbackManager.remove(opt) | ||
if (callbackManager.count() === 0) { | ||
scroller.unlisten(onScroll) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createCallbackManager, createScroller } from '../utils' | ||
|
||
/** | ||
* @typedef {Object} ReachBottomParam | ||
* @property {Function} callback | ||
* @property {*} ctx | ||
* @property {Number|undefined} onReachBottomDistance | ||
*/ | ||
|
||
/** | ||
* @param {ReachBottomParam} opt | ||
*/ | ||
export const onReachBottom = (opt) => { | ||
const callbackManager = createCallbackManager() | ||
const scroller = createScroller(opt.ctx) | ||
const distance = typeof opt.onReachBottomDistance === 'number' | ||
? opt.onReachBottomDistance | ||
: 50 | ||
|
||
let canTrigger = true | ||
|
||
const onScroll = () => { | ||
if (scroller.isReachBottom(distance)) { | ||
canTrigger && callbackManager.trigger() | ||
canTrigger = false | ||
} else { | ||
canTrigger = true | ||
} | ||
} | ||
|
||
callbackManager.add(opt) | ||
scroller.listen(onScroll) | ||
|
||
return () => { | ||
callbackManager.remove(opt) | ||
if (callbackManager.count() === 0) { | ||
scroller.unlisten(onScroll) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters