mirror of
https://github.com/marktext/marktext.git
synced 2025-05-04 05:42:58 +08:00
fix animated scrolling (#2780)
This commit is contained in:
parent
2287780248
commit
f89ec51938
@ -693,7 +693,13 @@ export default {
|
|||||||
this.editor.on('selectionChange', changes => {
|
this.editor.on('selectionChange', changes => {
|
||||||
const { y } = changes.cursorCoords
|
const { y } = changes.cursorCoords
|
||||||
if (this.typewriter) {
|
if (this.typewriter) {
|
||||||
animatedScrollTo(container, container.scrollTop + y - STANDAR_Y, 100)
|
const startPosition = container.scrollTop
|
||||||
|
const toPosition = startPosition + y - STANDAR_Y
|
||||||
|
|
||||||
|
// Prevent micro shakes and unnecessary scrolling.
|
||||||
|
if (Math.abs(startPosition - toPosition) > 2) {
|
||||||
|
animatedScrollTo(container, toPosition, 100)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to fix #628: auto scroll cursor to visible if the cursor is too low.
|
// Used to fix #628: auto scroll cursor to visible if the cursor is too low.
|
||||||
|
@ -19,14 +19,6 @@ export const delay = time => {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// help functions
|
|
||||||
const easeInOutQuad = function (t, b, c, d) {
|
|
||||||
t /= d / 2
|
|
||||||
if (t < 1) return c / 2 * t * t + b
|
|
||||||
t--
|
|
||||||
return -c / 2 * (t * (t - 2) - 1) + b
|
|
||||||
}
|
|
||||||
|
|
||||||
const ID_PREFEX = 'mt-'
|
const ID_PREFEX = 'mt-'
|
||||||
let id = 0
|
let id = 0
|
||||||
const DOTU = 'DOTU'
|
const DOTU = 'DOTU'
|
||||||
@ -160,35 +152,36 @@ export const animatedScrollTo = function (element, to, duration, callback) {
|
|||||||
const start = element.scrollTop
|
const start = element.scrollTop
|
||||||
const change = to - start
|
const change = to - start
|
||||||
const animationStart = +new Date()
|
const animationStart = +new Date()
|
||||||
let animating = true
|
|
||||||
let lastpos = null
|
|
||||||
|
|
||||||
const animateScroll = function () {
|
// Prevent animation on small steps
|
||||||
if (!animating) {
|
if (Math.abs(change) <= 6) {
|
||||||
|
element.scrollTop = to
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
requestAnimationFrame(animateScroll)
|
|
||||||
|
const easeInOutQuad = function (t, b, c, d) {
|
||||||
|
t /= d / 2
|
||||||
|
if (t < 1) return (c / 2) * t * t + b
|
||||||
|
t--
|
||||||
|
return (-c / 2) * (t * (t - 2) - 1) + b
|
||||||
|
}
|
||||||
|
|
||||||
|
const animateScroll = function () {
|
||||||
const now = +new Date()
|
const now = +new Date()
|
||||||
const val = Math.floor(easeInOutQuad(now - animationStart, start, change, duration))
|
const val = Math.floor(easeInOutQuad(now - animationStart, start, change, duration))
|
||||||
if (lastpos) {
|
|
||||||
if (lastpos === element.scrollTop) {
|
|
||||||
lastpos = val
|
|
||||||
element.scrollTop = val
|
element.scrollTop = val
|
||||||
} else {
|
|
||||||
animating = false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
lastpos = val
|
|
||||||
element.scrollTop = val
|
|
||||||
}
|
|
||||||
if (now > animationStart + duration) {
|
if (now > animationStart + duration) {
|
||||||
element.scrollTop = to
|
element.scrollTop = to
|
||||||
animating = false
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
requestAnimationFrame(animateScroll)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requestAnimationFrame(animateScroll)
|
requestAnimationFrame(animateScroll)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user