【发布时间】:2022-01-26 02:25:43
【问题描述】:
我想让div 始终滚动到内容的底部。我的解决方案适用于textarea,但不适用于div,我不知道为什么。
我有以下代码:
使用textarea:
var i = 0;
var txt = 'Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]'
function type(){
if (i < txt.length) {
document.querySelector('textarea').scrollTop = document.querySelector('textarea').scrollHeight
document.querySelector('textarea').innerHTML += txt.charAt(i);
i++;
setTimeout(type,1)
}
}
type()
<textarea ></textarea>
使用div:
var i = 0;
var txt = 'Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]'
function type(){
if (i < txt.length) {
document.querySelector('div').scrollTop = document.querySelector('div').scrollHeight
document.querySelector('div').innerHTML += txt.charAt(i);
i++;
setTimeout(type,1)
}
}
type()
div{
width:100px;
height:100px;
}
<div></div>
我发现如果我使用textarea 而不是div,scrollTop = scrollHeight 将起作用并且将始终滚动到底部,但如果我使用div,它将不起作用。
谁能向我解释为什么这不起作用?
【问题讨论】:
标签: javascript html css scroll