I created a menu with submenu and on each li element I attached with lottie.js a svg element with animation on mouseenter.
I try to implement in jQuery all of this with .each() function for not write the same code for each li.
The issue is that when I go hover a sub li also the parent li animation is activated and I don't really understand why, 'cause the class output is specific for the element with mouse hover.
This is the HTML output of the menu
<nav>
<div class="menu-main-menu-container">
<ul id="menu-main-menu" class="menu">
<li class="menu-item shoes">
<a class="menu-item-link" href="#">
<div id="item-shoes"><!-- SVG icon with animation --></div>
<span class="menulink">Shoes</span>
</a>
<ul class="sub-menu">
<li class="menu-item shoes-woman">
<a class="menu-item-link" href="#">
<div id="item-shoes-woman"><!-- SVG icon with animation --></div>
<span class="menulink">Shoes woman</span>
</a>
</li>
<li class="menu-item shoes-man">
<a class="menu-item-link" href="#">
<div id="item-shoes-man"><!-- SVG icon with animation --></div>
<span class="menulink">Shoes man</span>
</a>
</li>
</ul>
</li>
<li class="menu-item shirts">
<a class="menu-item-link" href="#">
<div id="item-shirts"><!-- SVG icon with animation --></div>
<span class="menulink">Shirts</span>
</a>
</li>
</ul>
</div>
</nav>
This is the jQuery code I try
var menu_array = ['shoes', 'shirts', 'shoes-woman', 'shoes-man'];
$.each(menu_array, function(key, value) {
var ID = document.getElementById('item-'+value);
var menuicon = lottie.loadAnimation({
container: ID,
renderer: 'svg',
loop: true,
autoplay: false,
path: basepath+'assets/img/'+value+'.json',
});
$('.'+value).on('mouseenter', function(){
console.log(this);
menuicon.goToAndPlay(1, true);
});
$('.'+value).on('mouseleave', function(){
menuicon.goToAndStop(1, true);
});
});
With this code I get the undesired result that if I go hover one sub li element, also the parent activate the animation.
I also tried to write the mouseenter and mouseleave events outside the each loop, but the result is the same.
I spend all my morning figured out where is the problem.
I only understood with the console.log that on sub li hover I get both li (parent and child), but I don't understand why 'cause the
$('.'+value).on('mouseenter', function(){
console.log(this);
menuicon.goToAndPlay(1, true);
});
must print
$('.shoes-man').on('mouseenter', function(){
console.log(this);
menuicon.goToAndPlay(1, true);
});
so only the selector .shoes-man must get the mouseenter event and not also the .shoes parent element.
Thank you