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

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

View file

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