-1

Hi I am trying to bind mouseenter event of jquery on two different elements on event firing div3 slide. But when cursor move from div1 to div2, div3 first slide up and then slide down.

Fiddle: http://jsfiddle.net/gzcnouux/

Expected Behaviour: Div only slide up when cursor is out of both the div

Master.Deep
  • 792
  • 5
  • 20

2 Answers2

2

There are a lot of different ways to accomplish this, but to illustrate the point, the mouseleave event is triggered before the mouseenter which causes the div to slide down and up. You can do a simple check on the relatedTarget.

if (e.relatedTarget && e.relatedTarget.id != 'd2' && e.relatedTarget.id != 'd1')
    $('#d3').slideUp();

ID Fiddle

A more maintainable way using classList and its contains method.

if (e.relatedTarget && !e.relatedTarget.classList.contains('box'))
    $('#d3').slideUp();

Class Fiddle

Namaskar
  • 2,114
  • 1
  • 15
  • 29
1

You can use mouseover event.

You should bind mouseover event for document DOM element and check if is not d1 or d2.

This can be achieved using event.target property in order to find out the DOM element that initiated the event.

$(document).on('mouseover', function(e) {
    if(e.target.id!='d1' && e.target.id!='d2')
       $('#d3').slideUp();
});

See full working solution.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128