繁体   English   中英

编辑内容可编辑的div时防止页面滚动

[英]Prevent page scrolling when editing a contenteditable div

请考虑以下示例,如此处所示: 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时箭头键不会滚动页面?

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

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

完成后,再次启用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM