diff --git a/src/components/d2-container/index.js b/src/components/d2-container/index.js index dcf493d4..18b5e271 100644 --- a/src/components/d2-container/index.js +++ b/src/components/d2-container/index.js @@ -64,6 +64,18 @@ export default { // 返回顶部 scrollToTop () { this.$refs.component.scrollToTop() + }, + // 用法同原生方法 scrollBy + scrollBy (x = 0, y = 0) { + this.$refs.component.$refs.body.scrollBy(x, y) + }, + // 用法同原生方法 scrollTo + scrollTo (x = 0, y = 0) { + this.$refs.component.$refs.body.scrollTo(x, y) + }, + // 用法同原生方法 scrollTop + scrollTop (top = 0) { + this.$refs.component.$refs.body.scrollTop = top } } } diff --git a/src/pages/demo/components/container/api.vue b/src/pages/demo/components/container/api.vue index 0f373d9d..30fb6f5e 100644 --- a/src/pages/demo/components/container/api.vue +++ b/src/pages/demo/components/container/api.vue @@ -1,19 +1,28 @@ @@ -69,6 +82,7 @@ export default { }, data () { return { + containerType: 'full', scrollDelay: 100, scrollTop: 0 } @@ -80,21 +94,14 @@ export default { } } }, - methods: { - handleScroll (e) { - this.scrollTop = e.target.scrollTop - }, - handleScrollToTop () { - this.$refs.container.scrollToTop() - }, - handleScrollBy () { - this.$refs.container.$children[0].$refs.body.scrollBy(0, 30) - }, - handleScrollTo () { - this.$refs.container.$children[0].$refs.body.scrollTo(0, 100) - }, - handleScrollTop () { - this.$refs.container.$children[0].$refs.body.scrollTop = 100 + 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)) } } }