【问题标题】:Prevent page scrolling when editing a contenteditable div编辑 contenteditable div 时防止页面滚动
【发布时间】:2014-08-05 16:09:10
【问题描述】:

考虑以下示例,如下所示:http://jsfiddle.net/Up38n/

<div contenteditable="true" style="background-color: #ccf;">
    <p>This div is editable.</p>
</div>
<div style="background-color: #ccf; width: 2000px;">
    <p>This is a wide div that causes scrollbars.</p>
</div>

单击以编辑第一段,然后按住右箭头键。最初,文本光标将按预期向右移动。但是,当文本光标到达段落末尾时,整个页面将向右滚动,您将无法再看到正在编辑的内容。

有没有办法防止这种行为,以便在编辑 div 时箭头键不会滚动页面?

【问题讨论】:

    标签: javascript jquery html google-chrome


    【解决方案1】:

    您可以暂时禁用文档的滚动:

    $(document).on('scroll touchmove mousewheel', function(e){
      e.preventDefault();
      e.stopPropagation();
      return false;
    });
    

    完成后,再次启用它:

    $(document).unbind('scroll touchmove mousewheel');
    

    【讨论】:

    • 滚动事件是在滚动和it cannot be cancelled之后触发的,所以我认为该策略没有任何作用。
    • 向上翻页也搞砸了整个 jsfiddle 页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多