To solve the problem, you need to analyze the direction of the mouse cursor movement: if the cursor moves vertically, then open menu items in normal mode, and if horizontally or diagonally, ignore the movement over adjacent menu items.
Determining the direction of the cursor can be implemented as follows. Add the following snippet before your code:
var prevPageX = 0; var isVerticalDirection = true; var directionTimer = 0; $('.mega-mnu ul').mousemove(function(e) { if (directionTimer) { return false; } directionTimer = setTimeout(function() { // в данном случае считается, что курсор движется не по вертикали, // если за время 50 миллисекунд его горизонтальное смещение больше 10 пикселей isVerticalDirection = Math.abs(e.pageX - prevPageX) < 10; clearTimeout(directionTimer); directionTimer = 0; prevPageX = e.pageX; }, 50); });
And change your code like this:
$('.mega-mnu ul li').hover(function () { if (isVerticalDirection) { $(this).css({'color':'#000'}); $(this).parent().find('li > ul:visible').hide(); var mega_inner = $(this).find('ul'); mega_inner.css({'display':'block'}); } }, function () { if (isVerticalDirection) { var mega_inner = $(this).find('ul'); mega_inner.css({'display':'none'}); } } );