简体   繁体   English

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

[英]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.

相关问题 如何防止从中删除“部分”<div contenteditable> 编辑的时候? - How to prevent removing "section" from <div contenteditable> when editing? 在 Div 元素内滚动时防止页面滚动 - Prevent page scrolling when scroll inside Div Element 当鼠标悬停在一个特定的div上时防止页面滚动 - Prevent page scrolling when mouse is over one particular div 滚动div元素时阻止页面滚动 - Prevent page scrolling while scrolling a div element React:编辑 contentEditable div 时如何保持插入符号位置? - React: How to maintain caret position when editing contentEditable div? 有没有办法防止contentEditable元素在光标到达底部时滚动? - Is there a way to prevent a contentEditable element from scrolling when the cursor reaches the bottom? 在contenteditable div中使用箭头键移动插入符号时,停止不必要的滚动 - Stop unwanted scrolling when moving caret with arrow keys in contenteditable div 解决控制台中的[Violation]警告时,滚动DIV时防止页面滚动 - Prevent page scrolling when scrolling a DIV while resolving [Violation] warning in console 如何防止div滚动出页面 - How to prevent div from scrolling out of page 如何在删除contenteditable div中的元素时防止插入错位 - How to prevent caret misplacement when deleting elements in a contenteditable div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM