简体   繁体   English

如何识别正在使用哪个元素滚动?

[英]How to identify which element scroll is being used?

I have a component in react, in which on click of a button ~ I display a drop down of a menu options. 我有一个组件在react中,单击一个按钮〜我会显示一个菜单选项的下拉列表。

Both menu overflow and browser screen overflow is enabled. 菜单溢出和浏览器屏幕溢出均已启用。

My requirement is that when the user is scrolling the menu. 我的要求是,当用户滚动菜单时。 No changes required. 无需更改。 However, if the menu is open and user tries to scroll the browser scroll - the menu should close. 但是,如果菜单已打开并且用户尝试滚动浏览器滚动条,则菜单应关闭。

WHAT AM I DOING TO ACHIEVE THIS? 我该怎么做?

  private handleMenu = (condition)? debounce(() => this.closeAllMenus(), 500, 
   {
    leading: true
   }) : debounce((event) => {}, 0, {});

Here, I am facing problems in finding the "condition" 在这里,我在寻找“条件”时遇到了问题

condition should equal to get the instance when user is scrolling the browser scroll. 条件应该等于在用户滚动浏览器滚动条时获取实例。

If true, I will close the menu dropdown for an interval. 如果为true,我将关闭菜单下拉菜单。 If false, (ie user is actually using scroll of the menu) - do nothing. 如果为false(即用户实际上正在使用菜单滚动),则什么也不做。

This is being called like this: - 这样称呼:-

private addEventListeners() {
    document.addEventListener("scroll", this.handleMenu, true);   
  }

  private removeEventListeners() {
    document.removeEventListener("scroll", this.handleMenu, true);
  }

FOLLOWING ARE THE CONDITIONS I HAVE TRIED "condition":- 以下是我尝试过的“条件”条件:

  1. window.scrollY - However, this does not tell me whether the user is currently scrolling it or not. window.scrollY-但是,这不会告诉我用户当前是否正在滚动它。

    It will only tell me the Y co-ordinate. 它只会告诉我Y坐标。

    How do I get to know which scroll is being used by currently. 我如何知道当前正在使用哪个滚动条。

  2. document.activeElement.mouseleave - but that would just tell me that mouse pointer is not on the menu. document.activeElement.mouseleave-但这只是告诉我菜单上没有鼠标指针。 But not that scroll of the browser is being used. 但是没有使用浏览器的滚动。

    How do I get to know which scroll is being used currently ? 我如何知道当前正在使用哪个滚动条?

  3. disable the browser scroll using document.body.style.overflow = "hidden". 使用document.body.style.overflow =“ hidden”禁用浏览器滚动。 However, this is not a recommended approach. 但是,这不是推荐的方法。

This image has a button in the bottom - 此图片的底部有一个按钮-

https://ibb.co/552TSnJ

On click of the button, a menu will open as shown in image - 单击该按钮后,将打开一个菜单,如下图所示-

https://ibb.co/BgsmYSm

How do I get to know if currently my browser scroll is being scrolled. 我如何知道当前是否正在滚动浏览器滚动条。

  private handleMenu = (condition)? debounce(() => this.closeAllMenus(), 500, 
   {
    leading: true
   }) : debounce((event) => {}, 0, {});

My requirement is just to close the menu when user tries to scroll the browser scroll and keep it open if user is trying to scroll the menu element scroll. 我的要求只是在用户尝试滚动浏览器滚动条时关闭菜单,并在用户试图滚动菜单元素滚动条时保持菜单打开。

EDIT: - 编辑:-

As suggested by one of the answers below, I changed my method to the following code: - 如以下答案之一所示,我将方法更改为以下代码:-

private handleMenu = (event: MouseEvent) => {
    event.preventDefault();
    window.onscroll = () => {
      this.closeAllMenus();
    }
  }

try make use of the 'scroll' event using 尝试通过以下方式使用“滚动”事件

window.addEventListener('scroll', yourFunction());

as per documentation, depending on the content of the given function you might want to throttle it for performance sake. 根据文档,根据给定功能的内容,可能出于性能考虑可能需要对其进行限制。 so you could for example use a timeout that prevents its executing for a given time in between calls. 因此,例如,您可以使用一个超时来阻止它在两次调用之间的给定时间内执行。

but using this event you know when the user is scrolling and just use a callback to close the given window. 但是使用此事件,您可以知道用户何时滚动并仅使用回调函数来关闭给定的窗口。

to register to scroll events on an element you can make use of the 'onscroll' attribute. 要注册滚动元素上的事件,可以使用'onscroll'属性。

Try using the class function ComponentDidMount() this will mount your code when components loads into the window. 尝试使用类函数ComponentDidMount(),这将在组件加载到窗口中时挂载代码。

constructor(props)
{
    super(props);
    this.state = ({
        scrollYnow: 0,
    });
    this.trackScrolling = this.trackScrolling.bind(this);
}
trackScrolling = () => {
    this.setState({scrollYnow: window.scrollY})
    console.log(this.state.scrollYnow);
}
componentDidMount() {

        document.addEventListener('scroll', this.trackScrolling);

}

This will update your function as the user scrolls, detects the scrollY but this can be changed with any scroll JS attribute hope this helps :) 这将在用户滚动时更新您的功能,检测到scrollY,但是可以使用任何滚动JS属性进行更改,希望这会有所帮助:)

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

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