当光标位于视口之外时,在Mathquill中,除非用户滚动,否则光标保持隐藏状态。通常,像在普通计算器视图中一样,mathfield应该使用光标自动滚动,以便始终将光标显示给用户。
其他数学编辑器的行为: https://i.imgur.com/1JbRv2F.gifv
Mathquill的行为: https://i.imgur.com/9E15uS2.gifv
我试图检查光标是否在视口之外,然后自动滚动,但问题是光标失去了焦点并变成了null元素,我无法滚动到它。
答案 0 :(得分:0)
我通过使用以下代码解决了这个问题:
首先,我添加了一个函数来检测光标是否在视口之外(源:https://stackoverflow.com/a/15203639/3880171)
function isElementVisible(el) {
var rect = el.getBoundingClientRect(),
vWidth = window.innerWidth || document.documentElement.clientWidth,
vHeight = window.innerHeight || document.documentElement.clientHeight,
efp = function (x, y) {
return document.elementFromPoint(x, y)
};
// Return false if it's not in the viewport
if (rect.right < 0 || rect.bottom < 0
|| rect.left > vWidth || rect.top > vHeight)
return false;
// Return true if any of its four corners are visible
return (
el.contains(efp(rect.left, rect.top))
|| el.contains(efp(rect.right, rect.top))
|| el.contains(efp(rect.right, rect.bottom))
|| el.contains(efp(rect.left, rect.bottom))
);
}
我将onKeyUp事件添加到mathview:
<span id="expression" onkeyup="onEditorKeyPress()"></span>
最后,执行滚动魔术的功能是:
function scrollToCursor() {
mathField.focus();
var cursor = document.querySelector(".mq-cursor");
if (cursor != null && !isElementVisible(cursor)) {
document.querySelector(".mq-cursor").scrollIntoView();
}
}
function onEditorKeyPress() {
scrollToCursor();
}