MouseEnter vs MouseOver in AngularJs

18,635

Angular's ngMouseenter directive fires an event whose type is mouseover, as you can see in this plunker.

The difference from ngMouseover is that it's fired only once - when mouse enters the element, not after every movement within this element too.

<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
</head>

<body ng-app="">
  <button ng-mouseenter="lastEventType=$event.type">
    Enter
  </button>
  Event type: {{lastEventType}}
</body>

</html>
Share:
18,635

Related videos on Youtube

Rohit
Author by

Rohit

JavaScript

Updated on September 15, 2022

Comments

  • Rohit
    Rohit over 1 year

    I was playing with AngularJS mouse events and got into a problem. I know MouseEnter event is fired when mouse enters container of an element and there after MouseOver is fired for all child elements. Thanks to this animation Visualizing mouse events

    However turns out that in my case MouseEnter event is never fired. I am working on Angular PhoneCat application (step-10) and did following modifications:

    1. Controllers.js: Added a method to log mouse events
    2. phone-details.html: Added ng-mouseenter and ng-mouseleave handlers

        $scope.logMouseEvent = function() {
            switch (event.type) {
              case "mouseenter":
                console.log("Hey Mouse Entered");
                break;
    
              case "mouseleave":
                console.log("Mouse Gone");
                break;
    
              default:
                console.log(event.type);
                break;
            }
    <ul class="phone-thumbs">
      <li ng-repeat="img in phone.images">
        <img ng-src="{{img}}" ng-Click="setImage(img)" ng-mouseenter="logMouseEvent()" ng-mouseleave="logMouseEvent()">
      </li>
    </ul>

    Result:

    Only mouseover and mouseout event being logged

    Question:

    Is this behavior happening because images are ul element and we are moving mouse in child elements? and How can I get mouseenter event on image?

    Thank you enter image description here

  • Rohit
    Rohit over 9 years
    thanks @audonex, but that's what I was trying to understand. Why your plunker never show mouseenter event? If both events are same then why there are two events for the same purpose?
  • audonex
    audonex over 9 years
    @Rohit I look at it the way that it's one event triggered by two different directives for two different purposes. There should be a reason why the type of mouseenter event is mouseover, but at the moment I can't think of any. IMHO there is nothing to understand, just accept the way it is. There should be no limitations caused by this.
  • Israel
    Israel about 8 years
    "after every movement within this element too" - this is not correct, movement doesn't fire ng-mouseover. Found the answer here: stackoverflow.com/questions/7286532/…