为什么在这里我没有滚动活动,而点击事件可能是我做错的事情
因此,这是代码,我想将滚动事件对象放在div的滚动中,其中的内容很大,因此DIV变得可以滚动我们如何获得滚动事件,以及为什么它不起作用
import React from "react";
function App() {
return (
<div onScroll={(e) => console.log(e)}>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, facilis
magni d.......
...............
vero!
</div>
);
}
export default App;
So here is the code and I want to get the scroll event object on the scroll of div which content is a lot so div becomes scrollable how we can get scroll event and why its not working
import React from "react";
function App() {
return (
<div onScroll={(e) => console.log(e)}>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore, facilis
magni d.......
...............
vero!
</div>
);
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是,您不是在滚动div,即
onscroll
on(从注释中的链接中,即pagesContainer
div),而是滚动你的整个身体。查看是否在onScroll
div下方添加另一个元素,因为您会在滚动时看到内容变化。在您共享的可重现示例中,您的.pagesContainer
div为500VW,因此您需要滚动该Div(即:正文)的容器才能查看内容。相反,您要保留.pagesContainer
屏幕的宽度(即:100VW
),但将每个页面(.pages
)放在容器中该宽度的Div Span100%
(可以使用flex:0 0 100%;
,请参见在这里)。这样,您将在pagesContainer
div“溢出”其宽度中具有元素。您需要确保向父添加Overflow-x:scroll;
,以指定当内容溢出而不是在容器之外渲染时,应添加滚动条。请参阅下面的示例(查看日志浏览器控制台):
Your issue is that you're not scrolling your div that you have
onScroll
on (from your link in the comments, that is thepagesContainer
div), but rather, are scrolling your entire body. It's easier to see if you add another element below youronScroll
div as you'll see that content shift as you scroll. In the reproducible example you shared, your.pagesContainer
div is 500vw, so you need to scroll the container of that div (ie: the body) to see the contents. Instead, you want to keep.pagesContainer
the width of your screen (ie:100vw
) but make each page (.pages
) within the container of your div span100%
of that width (this can be done by usingflex: 0 0 100%;
, see here). This way, you will have elements within yourpagesContainer
div "overflowing" its width. You need to ensure that you addoverflow-x: scroll;
to the parent to specify that it should add a scrollbar when the content overflows rather than render it outside of the container.See example below (view browser console for logs):
不确定您是要使元素本身滚动或只想让应用程序机。每当您滚动页面时log对象....
但是,如果您想安装对象。这样的文档...
请注意,您需要获取
getBoundingClientRect()
才能知道屏幕是否在元素上您想要的元素,每当您在元素上滚动窗口时,滚动函数都会工作
希望它能帮助您
not sure if you want to make the element itself scrollable or just want the app to console.log the object whenever you scroll the page....
but if you want to console.log the object anyway try to attach an event listener to the document like this ...
note that you need to get the
getBoundingClientRect()
to know if the screen is on the element you wantthis way whenever you scroll the window over the element, the scroll function will work
hope it helps you