[建议]事件执行由用户决定

This commit is contained in:
shawn.han 2019-10-17 13:38:17 +08:00
parent d78d57e581
commit 226a1e71ee
2 changed files with 12 additions and 8 deletions

View file

@ -48,6 +48,10 @@
},
offsetBottom: {
type: Number
},
useCapture: {
type: Boolean,
default: false
}
},
data () {
@ -78,8 +82,8 @@
mounted () {
// window.addEventListener('scroll', this.handleScroll, false);
// window.addEventListener('resize', this.handleScroll, false);
on(window, 'scroll', this.handleScroll);
on(window, 'resize', this.handleScroll);
on(window, 'scroll', this.handleScroll, this.useCapture);
on(window, 'resize', this.handleScroll, this.useCapture);
this.$nextTick(() => {
this.handleScroll();
});
@ -87,8 +91,8 @@
beforeDestroy () {
// window.removeEventListener('scroll', this.handleScroll, false);
// window.removeEventListener('resize', this.handleScroll, false);
off(window, 'scroll', this.handleScroll);
off(window, 'resize', this.handleScroll);
off(window, 'scroll', this.handleScroll, this.useCapture);
off(window, 'resize', this.handleScroll, this.useCapture);
},
methods: {
handleScroll () {

View file

@ -4,9 +4,9 @@ const isServer = Vue.prototype.$isServer;
/* istanbul ignore next */
export const on = (function() {
if (!isServer && document.addEventListener) {
return function(element, event, handler) {
return function(element, event, handler, useCapture = false) {
if (element && event && handler) {
element.addEventListener(event, handler, false);
element.addEventListener(event, handler, useCapture);
}
};
} else {
@ -21,9 +21,9 @@ export const on = (function() {
/* istanbul ignore next */
export const off = (function() {
if (!isServer && document.removeEventListener) {
return function(element, event, handler) {
return function(element, event, handler, useCapture = false) {
if (element && event) {
element.removeEventListener(event, handler, false);
element.removeEventListener(event, handler, useCapture);
}
};
} else {