gpt4 book ai didi

Javascript:在 div 上执行的 scrollBy 函数

转载 作者:可可西里 更新时间:2023-11-01 01:57:50 24 4
gpt4 key购买 nike

我有一个带有 overflow: scroll; 的 div,我想放一个按钮。当您按下它时,div 的内容会滚动。

类似于:

function scrollDiv() {
document.getElementById("d").scrollBy(100, 100);
}
<div id="d">
<p>Click the button to scroll by 100px.</p>
<button onclick="scrollDiv()">Click me to scroll!</button>
</div>

这在 FF 中有效,但在 Chrome 中无效。在 Chrome 中我得到

Uncaught TypeError: undefined is not a function

scrollBy 函数似乎被定义为 window 函数,所以我猜这就是问题所在,它在 FF 中工作而不是标准。

但是还有其他方法吗?我没有使用 jQuery,我宁愿不用。

最佳答案

你可以试试这个:

function scrollDiv(){
document.getElementById("d").scrollTop += 100;
document.getElementById("d").scrollLeft += 100;
}

关于Javascript:在 div 上执行的 scrollBy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com