Last active
May 3, 2016 07:41
-
-
Save Ginhing/cd3a3d9ff845c6d732f38742aa614f14 to your computer and use it in GitHub Desktop.
a vue-directive to fire when scroll at top or bottom
This file contains hidden or 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
'scroll-at': { | |
/** | |
* @example: <div v-scroll-at.bottom="loadMore"></div> | |
*/ | |
_bind() { | |
this.el.addEventListener('scroll', this.scrollHandler) | |
}, | |
_unbind() { | |
this.el.removeEventListener('scroll', this.scrollHandler) | |
}, | |
bind() { | |
let loading = false | |
let done = () => { loading = false } | |
this.scrollHandler = () => { | |
let triggerTop = this.modifiers.top || false | |
let distance = this.el.scrollTop | |
let scrollMax = this.el.scrollHeight - this.el.clientHeight | |
let trigger = triggerTop ? distance < 10 : distance > scrollMax - 10 | |
if (this.handler && !loading && trigger) { | |
loading = true | |
let result = this.handler() | |
if (result === undefined) done() | |
else if (result.then && result.then.call) result.then(done, done) | |
else done() | |
} | |
} | |
this._bind() | |
}, | |
update(value) { | |
this.handler = value | |
value.unbind = () => this._unbind() | |
value.bind = () => this._bind() | |
}, | |
unbind() { | |
this._unbind() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment