onMouseLeave和onMouseOut激活得太快;

时间:2018-06-06 08:22:03

标签: javascript reactjs extjs extreact

有没有办法降低触发事件onMouseLeave或onMouseOut的敏感度。因为我想将它们应用于弹出对话框,除非它们在鼠标悬停在文本上时一直触发(即div标签),只有当我完全平静下来然后它们才会触发并且弹出对话框没问题。功能性显然有效但鼠标事件的敏感性太高。

以下是我最重要的代码部分:

<div className="titleUnderLogo">
                <br /> <br />
                {applicationDataDefinition.map((item) =>
                    <div className="titleUnderLogo" onMouseOver = {this.mouseOver} onMouseLeave={this.mouseLeave} /* Tried with this also onMouseOut={this.mouseOut} */  > 

                        {item.name} {item.release} - {item.level} - {item.location}
                    </div>
                )}
                <br /> <br />
                <Container >
                    <Dialog 
                        displayed = {showDialog}
                        title="Product name"
                        modal={true}
                        layout="center"
                        defaultFocus="#ok"
                        ui="titleUnderLogoCls"
                        closeAction="hide"
                        maskTapHandler={this.mouseTapped}
                        onHide={() => this.setState({ showDialog: false })}
                    >
                        {applicationDataDefinition.map((item) => 
                            <div>
                                <table>
                                    <tr>
                                        <td> <b> Product name: </b> </td> 
                                        <td> {item.name} </td>
                                    </tr>
                                    <tr>
                                        <td> <b> Release: </b> </td>
                                        <td>  {item.release}  </td>
                                    </tr>
                                    <tr>
                                        <td> <b> Last fix installed: </b> </td> 
                                        <td> {item.lastFix}  </td>
                                    </tr>
                                    <tr>
                                        <td> <b> Environment: </b> </td>
                                        <td> {item.level} </td>
                                    </tr>
                                        <td> <b> Location: </b>  </td>
                                        <td> {item.location} </td>
                                </table>
                            </div>
                        )}
                    </Dialog>
                </Container>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用lodash.throttle

来限制事件
element.addEventListener('mouseout', _.throttle(callback, 100)); // 100 miliseconds

callback是你在mouseout上做的功能

修改

嗯,我不理解反应,但我可以看到必须像这样的一些

this.mouseOver //似乎是ViewModel中的方法

mouseOver: _.throttle(() => {
        // do something here or call another function like
        this.yourFunctionFromViewModelForMouseOver();
},100)

onMouseLeave={this.mouseLeave}

相同
mouseLeave: _.throttle(() => {
            // do something here or call another function like
            this.yourFunctionFromViewModelForMouseLeave();
    },100)

答案 1 :(得分:0)

尝试lodash.debounce =&gt; link

用法:

import _debounce from "lodash.debounce"
/* add this to countructor function or anywhere before you use mouseLeave function */
this.mouseLeave = _debounce(this.mouseLeave, 300); // 300 milliseconds