简体   繁体   English

我正在尝试使用此 JavaScript 行滚动 div 容器,但它不起作用。 为什么?

[英]I'am trying to scroll a div container with this JavaScript line, but it doesn't work. Why?

This is my code:这是我的代码:

 1. div.addEventListener(scroll, function(){
 2.       window.scrollBy(50, 0);
 3. });

But it does not scroll.但它不滚动。 What is the reason of this?这是什么原因?

You want a scroll on a div to trigger a window scroll.您希望在div上滚动以触发window滚动。 This is how you can do it:您可以这样做:

div.addEventListener("scroll", function() {
    window.scrollBy(50, 0);
});

I suspect the issue with your code is that window.scrollBy arguments is horizontal, then vertical.我怀疑您的代码的问题是window.scrollBy arguments 是水平的,然后是垂直的。

To scroll the page down by 50 pixels, you need window.scrollBy( 0, 50 ) .要将页面向下滚动 50 像素,您需要window.scrollBy( 0, 50 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM