[英]Add / remove a class, a time interval for all child elements when you hover on parent
There are some elements with child elements, when you hover, child elements should be added to the class after interval. 有一些带有子元素的元素,当您将鼠标悬停时,应在间隔之后将子元素添加到类中。
I can add the class was able to hover, and when I take away the mouse from the parent class is removed first, and then again for some reason added, I do not understand what was going on. 我可以添加该类能够悬停的类,并且当我从父类中拿走鼠标时,先将其删除,然后又由于某种原因再次将其删除,我不知道发生了什么。
$('.field_icon') .mouseover(function() { $(this).children('.field-item').each(function(i, el) { setTimeout(function() { $(el).addClass('active'); }, 100 + (i * 300)); }) }) .mouseleave(function() { $(this).children('.field-item').removeClass('active') })
.field-item { display: inline-block; width: 50px; height: 50px; background: #7CB342; } .field-item.active { background: #FF9800; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div>
You need to clear the timeout interval using clearTimeout
in the mouseleave
event; 您需要在
mouseleave
事件中使用clearTimeout
清除超时间隔;
//Declare an empty array to store the timeoutID var t = []; $('.field_icon') .mouseover(function() { $(this).children('.field-item').each(function(i, el) { //store the reference in a variable var t1 = setTimeout(function() { $(el).addClass('active'); }, 100 + (i * 300)); //Push it the array t.push(t1); }) }) .mouseleave(function() { //iterate and Clears the delay set setTimeout t.forEach(clearTimeout); //reset array to emepty t.length = 0; $(this).children('.field-item').removeClass('active') })
.field-item { display: inline-block; width: 50px; height: 50px; background: #7CB342; } .field-item.active { background: #FF9800; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> </div> <div class="field_icon"> <div class="field-item"></div> <div class="field-item"></div> <div class="field-item"></div> </div>
Use mouseenter , not mouseover: 使用mouseenter ,而不是mouseover:
http://www.w3schools.com/jquery/event_mouseover.asp http://www.w3schools.com/jquery/event_mouseover.asp
Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.
与mouseenter事件不同,mouseover事件会在鼠标指针输入任何子元素以及所选元素时触发。 The mouseenter event is only triggered when the mouse pointer enters the selected element.
仅当鼠标指针进入所选元素时才触发mouseenter事件。
So, essentially, when you move the mouse over the child elements, it re-adds all the events again as the parent element gets the child element moveover. 因此,从本质上讲,当您将鼠标移到子元素上时,当父元素获得子元素移动时,它将再次重新添加所有事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.