diff --git a/src/components/d2-container/components/d2-container-full.vue b/src/components/d2-container/components/d2-container-full.vue index bc66cc79..0c8a719a 100644 --- a/src/components/d2-container/components/d2-container-full.vue +++ b/src/components/d2-container/components/d2-container-full.vue @@ -17,9 +17,15 @@ import scroll from './mixins/scroll.normal' export default { name: 'd2-container-full', mixins: [ - scroll({ - ref: 'body' - }) - ] + scroll + ], + mounted () { + // 增加滚动事件监听 + this.addScrollListener() + }, + beforeDestroy () { + // 移除滚动事件监听 + this.removeScrollListener() + } } diff --git a/src/components/d2-container/components/mixins/scroll.normal.js b/src/components/d2-container/components/mixins/scroll.normal.js index 34e800d6..f057628b 100644 --- a/src/components/d2-container/components/mixins/scroll.normal.js +++ b/src/components/d2-container/components/mixins/scroll.normal.js @@ -3,24 +3,48 @@ import { throttle } from 'lodash' -/** - * 根据配置输出 mixin 设置 - * @param {String} ref 滚动容器 ref 名称 - */ -export default function ({ ref }) { - return { - data () { - return { - throttledHandleScroll: throttle(() => { - console.log(this.$refs[ref].scrollTop) - }, 300) +// 生成滚动事件的 handler +function handleMaker (wait) { + return throttle(e => this.$emit('scroll', e), wait) +} + +export default { + props: { + // 滚动事件节流间隔 + scrollDelay: { + type: Number, + required: false, + default: 100 + } + }, + data () { + return { + handleScroll: null + } + }, + watch: { + scrollDelay (val) { + // 移除旧的监听 + this.removeScrollListener() + // 生成新的 handle 方法 + this.handleScroll = handleMaker.call(this, val) + // 添加新的监听 + this.addScrollListener() + } + }, + methods: { + // 增加滚动事件监听 + addScrollListener () { + if (typeof this.handleScroll !== 'function') { + // mounted 生命周期内调用这个方法的时候会进入这里的判断 + this.handleScroll = handleMaker.call(this, this.scrollDelay) } + // 添加监听 + this.$refs.body.addEventListener('scroll', this.handleScroll) }, - mounted () { - this.$refs[ref].addEventListener('scroll', this.throttledHandleScroll) - }, - beforeDestroy () { - this.$refs[ref].removeEventListener('scroll', this.throttledHandleScroll) + // 移除滚动事件监听 + removeScrollListener () { + this.$refs.body.removeEventListener('scroll', this.handleScroll) } } } diff --git a/src/components/d2-container/index.vue b/src/components/d2-container/index.vue index 17dbbebc..d975ce03 100644 --- a/src/components/d2-container/index.vue +++ b/src/components/d2-container/index.vue @@ -1,7 +1,7 @@