diff --git a/src/components/d2-container/components/mixins/bs.js b/src/components/d2-container/components/mixins/bs.js index a457a69a..50a74723 100644 --- a/src/components/d2-container/components/mixins/bs.js +++ b/src/components/d2-container/components/mixins/bs.js @@ -21,6 +21,7 @@ export default { }, methods: { scrollInit () { + // 初始化 bs this.BS = new BScroll(this.$refs.wrapper, Object.assign({ mouseWheel: true, scrollbar: { @@ -28,6 +29,11 @@ export default { interactive: false } }, this.betterScrollOptions)) + // 滚动时发出事件 并且统一返回的数据格式 + this.BS.on('scroll', ({x, y}) => this.$emit('scroll', { + x: -x, + y: -y + })) }, scrollDestroy () { // https://github.com/d2-projects/d2-admin/issues/75 @@ -37,6 +43,19 @@ export default { delete this.BS this.BS = null } + }, + // 外部调用的方法 返回顶部 + scrollToTop () { + if (this.BS) this.BS.scrollTo(0, 0, 300) + }, + // 手动发出滚动事件 + scroll () { + if (this.BS) { + this.$emit('scroll', { + x: -this.BS.x, + y: -this.BS.y + }) + } } } } diff --git a/src/components/d2-container/components/mixins/normal.js b/src/components/d2-container/components/mixins/normal.js index 317132c0..d71ff0b8 100644 --- a/src/components/d2-container/components/mixins/normal.js +++ b/src/components/d2-container/components/mixins/normal.js @@ -5,7 +5,12 @@ import { throttle } from 'lodash' // 生成滚动事件的 handler function handleMaker (wait) { - return throttle(e => this.$emit('scroll', e), wait) + return throttle(e => { + this.$emit('scroll', { + x: e.target.scrollLeft, + y: e.target.scrollTop + }) + }, wait) } export default { @@ -14,7 +19,7 @@ export default { scrollDelay: { type: Number, required: false, - default: 100 + default: 10 } }, data () { diff --git a/src/components/d2-container/index.js b/src/components/d2-container/index.js index 18b5e271..e03b6c59 100644 --- a/src/components/d2-container/index.js +++ b/src/components/d2-container/index.js @@ -64,18 +64,48 @@ export default { // 返回顶部 scrollToTop () { this.$refs.component.scrollToTop() + // 如果开启了 better scroll 还需要手动触发一遍 scroll 事件 + const bs = this.$refs.component.BS + if (bs) this.$refs.component.scroll() }, // 用法同原生方法 scrollBy - scrollBy (x = 0, y = 0) { - this.$refs.component.$refs.body.scrollBy(x, y) + scrollBy (x = 0, y = 0, time = 300) { + if (this.betterScroll) { + const bs = this.$refs.component.BS + if (bs) { + bs.scrollBy(-x, -y, time) + // 手动触发一遍 scroll 事件 + this.$refs.component.scroll() + } + } else { + this.$refs.component.$refs.body.scrollBy(x, y) + } }, // 用法同原生方法 scrollTo - scrollTo (x = 0, y = 0) { - this.$refs.component.$refs.body.scrollTo(x, y) + scrollTo (x = 0, y = 0, time = 300) { + if (this.betterScroll) { + const bs = this.$refs.component.BS + if (bs) { + bs.scrollTo(-x, -y, time) + // 手动触发一遍 scroll 事件 + this.$refs.component.scroll() + } + } else { + this.$refs.component.$refs.body.scrollTo(x, y) + } }, // 用法同原生方法 scrollTop - scrollTop (top = 0) { - this.$refs.component.$refs.body.scrollTop = top + scrollTop (top = 0, time = 300) { + if (this.betterScroll) { + const bs = this.$refs.component.BS + if (bs) { + bs.scrollTo(bs.x, -top, time) + // 手动触发一遍 scroll 事件 + this.$refs.component.scroll() + } + } else { + this.$refs.component.$refs.body.scrollTop = top + } } } } diff --git a/src/menu/modules/demo-components.js b/src/menu/modules/demo-components.js index 079184a1..0773964a 100644 --- a/src/menu/modules/demo-components.js +++ b/src/menu/modules/demo-components.js @@ -36,7 +36,8 @@ export default { { title: '方法', children: [ - { path: `${pre}container/api`, title: 'API' } + { path: `${pre}container/api?bs=false`, title: '滚动控制' }, + { path: `${pre}container/api?bs=true`, title: '滚动控制 BS' } ] } ] diff --git a/src/pages/demo/components/container/api.vue b/src/pages/demo/components/container/api.vue index 30fb6f5e..71b38345 100644 --- a/src/pages/demo/components/container/api.vue +++ b/src/pages/demo/components/container/api.vue @@ -2,8 +2,9 @@ + @scroll="({x, y}) => { scrollTop = y }"> 55 ? '1' : '.1' } } - }, - watch: { - containerType (val, oldVal) { - let top = this.scrollTop - // 因为 ghost 模式下的容器没有 20px 的 padding - // 为了保持垂直位置 需要重新计算定位高度 - if (oldVal === 'ghost') top += 20 - if (val === 'ghost') top -= 20 - this.$nextTick(() => this.$refs.container.scrollTo(0, top)) - } } }