当游标在视口之外时,在Mathquill中,光标将保持隐藏状态,除非用户滚动。通常情况下,就像在普通计算器视图中一样,mathfield应该与光标一起自动滚动,因此光标总是显示给用户。
其他数学编辑器的行为:https://i.imgur.com/1JbRv2F.gifv
Mathquill的行为:https://i.imgur.com/9E15uS2.gifv
我试着检查游标是否在外部视图,然后我自动滚动,但问题是光标失去焦点,变成空元素,我不能滚动到它。
发布于 2018-11-26 15:57:35
我使用以下代码解决了这个问题:
首先,我添加了一个函数来检测游标是否在视图之外(源: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))
);
}我在mathview中添加了onKeyUp事件:
<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();
}https://stackoverflow.com/questions/53467285
复制相似问题