OnMouseOut event not triggered when moving mouse fast (GWT - all browsers)

别等时光非礼了梦想. 提交于 2019-12-04 16:24:38

EDIT

Given your modified question and the added complexity of changing text, let's use GWT as GWT is awesome for this kind of thing!

Ok, first our very simple CSS stylesheet:

.myStyle {
background-color: blue;
}

.myStyle-clicked {
background-color: red;
}

Here a very basic Widget that does pretty much exactly what you asked (sorry for changing the text, I tested this and I'm sure it will always work even when moving the mouse extremely fast) in beautiful, simple Java (GWT) code:

private class MyWidget extends Composite {

    private Label label = new Label();
    private static final String originalText = "Hello world!";
    private static final String clickedText = "Goodbye world!";

    public MyWidget() {
        sinkEvents(Event.ONCLICK | Event.ONMOUSEOUT);
        label.setText(originalText);
        initWidget(label);
        setStyleName("myStyle");
    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        switch (event.getTypeInt()) {
        case Event.ONCLICK:
            addStyleDependentName("clicked");
            label.setText(clickedText);
            break;
        case Event.ONMOUSEOUT:
            removeStyleDependentName("clicked");
            label.setText(originalText);
            break;
        }
    }

}

OLD ANSWER IF YOU ARE JUST WORRIED ABOUT MOUSE_OVER AND MOUSE_OUT

The solution to this problem is to stop doing this programmatically and do it using the native browser's events handling system, which is as fast as you can get.

To do this, use the CSS hover filter. For clicking, you don't need to worry, your problem is just move-in and move-out, which as you found out, are cases where you may not be able to trust the JS to handle very well. I think all browsers currently support this:

<!DOCTYPE html>

<html>
  <head>
   <style>
    .tt {
       background-color: blue;
    }
    .tt:hover {
       background-color: red;
    }
   </style>
  </head>
  <body>
    <div class="tt">
        The content of the body element is displayed in your browser.
    </div>
  </body>
</html>

I tested this and it works in Chrome, FF and IE9. According to ther w3schools docs, it works also in Safari and Opera.

I am not in java, but i would just suggest to check if there is some other script you may have in your app that interfere with your code.

Perhaps try to isolate the code and execute it without anything else and see what happens?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!