首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使mathquill字段始终与光标一起滚动

使mathquill字段始终与光标一起滚动
EN

Stack Overflow用户
提问于 2018-11-25 12:09:15
回答 1查看 164关注 0票数 0

当游标在视口之外时,在Mathquill中,光标将保持隐藏状态,除非用户滚动。通常情况下,就像在普通计算器视图中一样,mathfield应该与光标一起自动滚动,因此光标总是显示给用户。

其他数学编辑器的行为:https://i.imgur.com/1JbRv2F.gifv

Mathquill的行为:https://i.imgur.com/9E15uS2.gifv

我试着检查游标是否在外部视图,然后我自动滚动,但问题是光标失去焦点,变成空元素,我不能滚动到它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-26 15:57:35

我使用以下代码解决了这个问题:

首先,我添加了一个函数来检测游标是否在视图之外(源:https://stackoverflow.com/a/15203639/3880171)

代码语言:javascript
复制
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>

最后,发挥滚动魔力的功能是:

代码语言:javascript
复制
function scrollToCursor() {
    mathField.focus();
    var cursor = document.querySelector(".mq-cursor");
    if (cursor != null && !isElementVisible(cursor)) {
        document.querySelector(".mq-cursor").scrollIntoView();
    }
}

function onEditorKeyPress() {
    scrollToCursor();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53467285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档