From 52e1afa2f246f4a003c0c31a8b226809cde5ac67 Mon Sep 17 00:00:00 2001 From: "lei.jiang" Date: Fri, 1 Dec 2017 11:13:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=8F=B3=E9=94=AEtag?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=85=B3=E9=97=AD=E9=80=89=E9=A1=B9=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=202.=E5=A2=9E=E5=8A=A0tagview=E7=9A=84?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=9D=A1=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ScrollPane/index.vue | 86 +++++++++++++---- src/views/layout/components/TagsView.vue | 115 ++++++++++++----------- 2 files changed, 128 insertions(+), 73 deletions(-) diff --git a/src/components/ScrollPane/index.vue b/src/components/ScrollPane/index.vue index ea13176d..1f633658 100644 --- a/src/components/ScrollPane/index.vue +++ b/src/components/ScrollPane/index.vue @@ -3,6 +3,10 @@
+
+
+
+
@@ -11,41 +15,83 @@ export default { name: 'scrollPane', data() { return { - left: 0 + left: 0, + isOverflow: false, + container: undefined, + containerWidth: undefined, + wrapper: undefined, + wrapperWidth: undefined, + scrollbarContainer: undefined, + scrollbarContainerWidth: undefined, + scrollbar: undefined, + scrollbarWidth: undefined } }, + mounted() { + this.container = this.$refs.scrollContainer + this.wrapper = this.$refs.scrollWrapper + this.scrollbarContainer = this.$refs.scrollbarContainer + this.scrollbar = this.$refs.scrollbar + this.calcSize() + window.addEventListener('resize', () => { + this.$forceUpdate() + this.calcSize() + }, false) + }, methods: { + calcPos() { + if (this.isOverflow) { + this.scrollbar.style.left = -(this.scrollbarContainerWidth - this.scrollbarWidth) * this.left / (this.wrapperWidth - this.containerWidth + 100) + 'px' + } + }, + calcSize() { + // 计算滚动条的长度 + this.containerWidth = parseFloat(this.container.offsetWidth, 10) + this.wrapperWidth = parseFloat(this.wrapper.offsetWidth, 10) + this.scrollbarContainerWidth = parseFloat(this.scrollbarContainer.offsetWidth, 10) + this.isOverflow = this.wrapperWidth + 100 > this.containerWidth + if (!this.isOverflow) return false + this.scrollbarWidth = this.scrollbarContainerWidth * this.containerWidth / (this.wrapperWidth + 100) + this.scrollbar.style.width = this.scrollbarWidth + 'px' + this.calcPos() + }, handleScroll(e) { e.preventDefault() - const $container = this.$refs.scrollContainer - const $containerWidth = $container.offsetWidth - const $wrapper = this.$refs.scrollWrapper - const $wrapperWidth = $wrapper.offsetWidth if (e.wheelDelta > 0) { + // 滚轮下滑 this.left = Math.min(0, this.left + e.wheelDelta) } else { - if ($containerWidth - 100 < $wrapperWidth) { - if (this.left < -($wrapperWidth - $containerWidth + 100)) { - this.left = this.left - } else { - this.left = Math.max(this.left + e.wheelDelta, $containerWidth - $wrapperWidth - 100) - } - } else { - this.left = 0 + // 滚轮上滑 + // 可滑动区间[-(this.wrapperWidth-this.containerWidth+100),0] + if (this.left >= -(this.wrapperWidth - this.containerWidth + 100)) { + this.left = Math.max(this.left + e.wheelDelta, this.containerWidth - this.wrapperWidth - 100) } } + this.calcPos() } } } diff --git a/src/views/layout/components/TagsView.vue b/src/views/layout/components/TagsView.vue index ec3acdf0..ce62f800 100644 --- a/src/views/layout/components/TagsView.vue +++ b/src/views/layout/components/TagsView.vue @@ -1,15 +1,17 @@