简体   繁体   English

TypeScript 和 Reactjs 中的滚动事件类型是什么

[英]What will be Scroll Event Type in TypeScript and Reactjs

I know we can use event listener and work with ScrollEvent.我知道我们可以使用事件侦听器并使用 ScrollEvent。

But suppose following method:但是假设以下方法:

handleScroll =(event:any):void=>{
const target = event.target;
// further code goes her
}

I want to use event type instead of any so what should be its type?我想使用事件类型而不是任何类型,那么它的类型应该是什么?

if you aim to catch the mouse-wheel scroll event then you can use this type.如果您的目标是捕捉鼠标滚轮滚动事件,那么您可以使用这种类型。

handleScroll = (event: WheelEvent): void =>

If you are not doing this in React then you would use如果您不在 React 中执行此操作,那么您将使用

handleScroll = (event: Event):void => {
  const target = event.target;
  // further code goes here
}

If you using React and the scrolling is being done in a <div> then you would use如果您使用 React 并且滚动是在<div>中完成的,那么您将使用

handleScroll = (event: React.UIEvent<HTMLDivElement>):void => {
  const target = event.target;
  // further code goes here
}

If the event is occuring on another type of element (and using React) then you should substitute that for <HTMLDivElement> .如果事件发生在另一种类型的元素上(并使用 React),那么您应该用它替换<HTMLDivElement>

As per a comment I wrote below another post here, you should not confuse wheel and scroll events.根据我在另一篇文章下面写的评论,你不应该混淆wheelscroll事件。

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

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