2019-11-01 17:16:40 +08:00
|
|
|
// 左右栏同步滚动
|
2019-11-10 15:52:46 +08:00
|
|
|
$(document).ready(() => {
|
2020-02-11 17:51:04 +08:00
|
|
|
let timeout
|
|
|
|
$('div.CodeMirror-scroll, #preview').on('scroll', function callback () {
|
|
|
|
clearTimeout(timeout)
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2020-02-11 17:51:04 +08:00
|
|
|
let source = $(this)
|
|
|
|
let target = $(source.is('#preview') ? 'div.CodeMirror-scroll' : '#preview')
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2020-02-11 17:51:04 +08:00
|
|
|
target.off('scroll')
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2020-02-11 17:51:04 +08:00
|
|
|
let source0 = source[0]
|
|
|
|
let target0 = target[0]
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2020-02-11 17:51:04 +08:00
|
|
|
let percentage = source0.scrollTop / (source0.scrollHeight - source0.offsetHeight)
|
|
|
|
let height = percentage * (target0.scrollHeight - target0.offsetHeight)
|
|
|
|
target0.scrollTo(0, height)
|
2019-11-01 17:16:40 +08:00
|
|
|
|
2019-11-10 15:52:46 +08:00
|
|
|
timeout = setTimeout(() => {
|
2020-02-11 17:51:04 +08:00
|
|
|
target.on('scroll', callback)
|
|
|
|
}, 100)
|
|
|
|
})
|
|
|
|
})
|