From 775dffe995e1f4a8a1ec28e128de30fd84f4ed19 Mon Sep 17 00:00:00 2001 From: JimQing Date: Mon, 6 Jul 2020 23:22:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E9=99=A4jquery=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/view/CodemirrorEditor.vue | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/view/CodemirrorEditor.vue b/src/view/CodemirrorEditor.vue index 451b831..4367de6 100644 --- a/src/view/CodemirrorEditor.vue +++ b/src/view/CodemirrorEditor.vue @@ -202,26 +202,23 @@ export default { // 左右栏同步滚动 leftAndRightScroll() { const _this = this; + const previewRef = document.getElementById('preview'); - document.getElementById('preview').addEventListener("scroll", function callback() { + previewRef.addEventListener("scroll", function callback() { clearTimeout(_this.timeout) - let source = $(_this) - let target = $(source.is('#preview') ? 'div.CodeMirror-scroll' : '#preview') + let source = this + let target = this.id === 'preview' ? document.getElementsByClassName('CodeMirror-scroll')[0] : previewRef; + + target.removeEventListener("scroll", callback, false); + let percentage = source.scrollTop / (source.scrollHeight - source.offsetHeight) + let height = percentage * (target.scrollHeight - target.offsetHeight) + target.scrollTo(0, height) - target.off('scroll') - - let source0 = source[0] - let target0 = target[0] - - let percentage = source0.scrollTop / (source0.scrollHeight - source0.offsetHeight) - let height = percentage * (target0.scrollHeight - target0.offsetHeight) - target0.scrollTo(0, height) - - _this.timeout = setTimeout(() => { - target.on('scroll', callback) - }, 100) - }, true); + _this.timeout = setTimeout(()=> { + target.addEventListener("scroll", callback, false); + }, 300) + }, false); }, onEditorRefresh() { this.editorRefresh(); @@ -236,7 +233,7 @@ export default { 'editorRefresh', 'initCssEditorEntity']) }, mounted() { - this.leftAndRightScroll() + this.leftAndRightScroll(); } }