4

有没有办法降低触发事件 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>
4

2 回答 2

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)
于 2018-06-06T08:27:24.367 回答
0

试试lodash.debounce=>链接

用法:

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
于 2018-06-06T08:42:11.613 回答