[improvement] add bind event mixin

This commit is contained in:
陈嘉涵
2019-06-03 17:46:14 +08:00
parent 884f624a16
commit 616db0114e
4 changed files with 56 additions and 82 deletions
+12 -27
View File
@@ -1,14 +1,24 @@
import { use } from '../utils';
import { TouchMixin } from '../mixins/touch';
import { ParentMixin } from '../mixins/relation';
import { BindEventMixin } from '../mixins/bind-event';
import { GREEN } from '../utils/color';
import { on, off } from '../utils/event';
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/scroll';
const [sfc, bem] = use('index-bar');
export default sfc({
mixins: [TouchMixin, ParentMixin('vanIndexBar')],
mixins: [
TouchMixin,
ParentMixin('vanIndexBar'),
BindEventMixin(function (bind) {
if (!this.scroller) {
this.scroller = getScrollEventTarget(this.$el);
}
bind(this.scroller, 'scroll', this.onScroll);
})
],
props: {
sticky: {
@@ -56,32 +66,7 @@ export default sfc({
}
},
mounted() {
this.scroller = getScrollEventTarget(this.$el);
this.handler(true);
},
destroyed() {
this.handler(false);
},
activated() {
this.handler(true);
},
deactivated() {
this.handler(false);
},
methods: {
handler(bind) {
/* istanbul ignore else */
if (this.binded !== bind) {
this.binded = bind;
(bind ? on : off)(this.scroller, 'scroll', this.onScroll);
}
},
onScroll() {
if (!this.sticky) {
return;