Javascript:在 div 上执行的 scrollBy 函数 [英] Javascript: scrollBy function executed on a div
问题描述
我有一个带有 overflow: scroll;
的 div,我想放一个按钮.当你按下它时,div
的内容会滚动.
类似于:
function scrollDiv() {document.getElementById("d").scrollBy(100, 100);}
<p>单击按钮可滚动 100 像素.</p><button onclick="scrollDiv()">点击我滚动!</button>
这适用于 FF,但不适用于 Chrome.在 Chrome 中我得到
<块引用>未捕获的类型错误:未定义不是函数
scrollBy
函数似乎被定义为 window
函数,所以我猜这就是问题所在,它在 FF 中不符合标准.
但是还有其他方法可以做到这一点吗?我没有使用 jQuery,我宁愿不使用.
你可以试试这个:
function scrollDiv(){document.getElementById("d").scrollTop += 100;document.getElementById("d").scrollLeft += 100;}
I have a div with overflow: scroll;
And I would like to put a button. When you press it, the content of the div
scrolls.
Something like:
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>
This works in FF, but not in Chrome. In Chrome I get
Uncaught TypeError: undefined is not a function
The scrollBy
function seems to be defined as window
function, so I guess this is the problem, and it's working in FF not by standard.
But is there another way to do that? I am not using jQuery, and I'd rather not.
You can try this:
function scrollDiv(){
document.getElementById("d").scrollTop += 100;
document.getElementById("d").scrollLeft += 100;
}
这篇关于Javascript:在 div 上执行的 scrollBy 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!