fix: scroll mehod reword

This commit is contained in:
JimQing 2020-07-12 18:33:46 +08:00
parent b8a99e75cf
commit 67c38d8127

View File

@ -19,7 +19,13 @@
<textarea id="editor" type="textarea" placeholder="Your markdown text here." v-model="source"> <textarea id="editor" type="textarea" placeholder="Your markdown text here." v-model="source">
</textarea> </textarea>
</el-col> </el-col>
<el-col :span="12" class="preview-wrapper" id="preview" :class="{'preview-wrapper_night': nightMode && isCoping}"> <el-col
:span="12"
class="preview-wrapper"
id="preview"
ref="preview"
:class="{'preview-wrapper_night': nightMode && isCoping}"
>
<section id="output-wrapper" :class="{'output_night': nightMode && !backLight}"> <section id="output-wrapper" :class="{'output_night': nightMode && !backLight}">
<div class="preview"> <div class="preview">
<section id="output" v-html="output"> <section id="output" v-html="output">
@ -193,26 +199,41 @@ export default {
}) })
} }
}, },
//
leftAndRightScroll() { leftAndRightScroll() {
const _this = this; const scrollCB = text=> {
const previewRef = document.getElementById('preview'); let source, target;
previewRef.addEventListener("scroll", function callback() { clearTimeout(this.timeout);
clearTimeout(_this.timeout) if (text === 'preview') {
source = this.$refs.preview.$el;
target = document.getElementsByClassName('CodeMirror-scroll')[0];
this.editor.off('scroll', editorScrollCB);
this.timeout = setTimeout(()=> {
this.editor.on('scroll', editorScrollCB);
}, 300);
} else if (text === 'editor') {
source = document.getElementsByClassName('CodeMirror-scroll')[0];
target = this.$refs.preview.$el;
target.removeEventListener("scroll", previewScrollCB, false);
this.timeout = setTimeout(()=> {
target.addEventListener("scroll", previewScrollCB, false);
}, 300);
}
let source = this let percentage = source.scrollTop / (source.scrollHeight - source.offsetHeight);
let target = this.id === 'preview' ? document.getElementsByClassName('CodeMirror-scroll')[0] : previewRef; let height = percentage * (target.scrollHeight - target.offsetHeight);
target.removeEventListener("scroll", callback, false);
let percentage = source.scrollTop / (source.scrollHeight - source.offsetHeight)
let height = percentage * (target.scrollHeight - target.offsetHeight)
target.scrollTo(0, height)
_this.timeout = setTimeout(()=> { target.scrollTo(0, height);
target.addEventListener("scroll", callback, false); };
}, 300) const editorScrollCB = ()=> {
}, false); scrollCB('editor');
};
const previewScrollCB = ()=> {
scrollCB('preview');
};
this.$refs.preview.$el.addEventListener("scroll", previewScrollCB, false);
this.editor.on('scroll', editorScrollCB);
}, },
onEditorRefresh() { onEditorRefresh() {
this.editorRefresh(); this.editorRefresh();
@ -232,8 +253,8 @@ export default {
'initCssEditorEntity']) 'initCssEditorEntity'])
}, },
mounted() { mounted() {
this.leftAndRightScroll();
setTimeout(() => { setTimeout(() => {
this.leftAndRightScroll();
PR.prettyPrint() PR.prettyPrint()
}, 300); }, 300);
} }