javascript - 如何防止自动滚动到重点内容可编辑的 div?
问题描述
input
并且textarea
像我想要的那样工作,但contentEditable
工作方式不同:当页面滚动时,contentEditable
焦点和新元素在contentEditable
浏览器自动移动滚动之前添加。
如何预防?为什么会这样?
setInterval(()=> {
const div = document.createElement('div');
div.className = "item";
document.querySelector('.items').appendChild(div);
}, 1000);
window.scrollTo(0, 10);
document.querySelector('div[contentEditable]').focus();
.scroll {
height: 2000px;
}
.item {
height: 20px;
background: red;
margin-bottom: 2px;
width: 100px;
}
<div class="scroll">
<div class="items">
<div class="item"></div>
</div>
<div><div contentEditable="true">Put focus here (ContentEditable)</div></div>
<div><input type="text" value="Input type text"></div>
<div><textarea>TextArea</textarea></div>
</div>
PS macOS Safari 像我想要的那样工作,Chrome/FireFox 重复这种情况。
解决方案
推荐阅读
- python - Tensorflow中的复值分布?
- roslyn-code-analysis - GetChangedDocumentAsync 返回 null 但应该有一个值
- rust - 是否可以连接迭代器?
- arrays - 根据字符串输出引用数组
- flutter - 如何使用 Flutter 实现像 Instagram live 这样的直播?
- python - 如何在 tkinter gui 上设置轮流(人类与计算机)玩 tictactoe?
- sysml - 使用 SysML 中的接口模块对信号进行建模
- swift - 斯威夫特:类型别名和关联类型与协议中的值有什么区别?
- c# - 编译器错误地指示使用未分配的局部变量错误
- c - 我的 C 程序因未知原因而崩溃