[英]Prevent page scrolling when editing a contenteditable div
Consider the following example, as demonstrated here: http://jsfiddle.net/Up38n/ 请考虑以下示例,如此处所示: 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>
Click to edit the first paragraph, and then hold down the right arrow key. 单击以编辑第一段,然后按住向右箭头键。 Initially the text cursor will move to the right as expected.
最初,文本光标将按预期向右移动。 However, when the text cursor hits the end of the paragraph, the whole page will scroll to the right and you will no longer be able to see what you're editing.
但是,当文本光标移至该段落的末尾时,整个页面将向右滚动,您将无法再看到正在编辑的内容。
Is there a way to prevent this behavior so that the arrow keys don't scroll the page while editing a div? 有没有一种方法可以防止这种行为,以便在编辑div时箭头键不会滚动页面?
You can temporarily disable the scroll for the document: 您可以暂时禁用文档的滚动:
$(document).on('scroll touchmove mousewheel', function(e){
e.preventDefault();
e.stopPropagation();
return false;
});
And when done, enable it again: 完成后,再次启用它:
$(document).unbind('scroll touchmove mousewheel');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.