简体   繁体   English

添加/删除类,将鼠标悬停在父类上时所有子元素的时间间隔

[英]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.

相关问题 从所有子元素中删除类,并在单击的Jquery的父/父上添加类 - Remove class from all child elements and add class on parent/parent of clicked Jquery Vanilla JS - 可以在子元素上添加/删除类,但所有类都处于活动状态 - Vanilla JS - Can add/remove class on child elements but all are active 单击子 li 时将类删除/添加到父 div - remove/add class to parent div when child li is clicked 删除父项和所有子项不起作用 - Remove parent and all child elements not working 如果任何子元素具有某个类,则将类添加到父div,否则将其删除(jQuery或Javascript) - Add class to parent div if any of the child elements has a certain class, remove if not (jQuery or Javascript) 向子元素和父元素添加和删除 class - Add and Remove class to child and parent element 当您在反应中单击父元素时是否可以 select 子元素,并在事件发生后将 class 添加到父元素 - is it possible to select child element when you click on parent element in react, and the add a class to the parent element after an event 当 hover 在另一个子容器上时,使用 javascript 容器闪烁并同时删除和添加 CSS 类 - using javascript container blinking and remove and add CSS classes at same time when hover over another child container 将鼠标悬停在元素上时如何突出显示具有相同 css 类的所有元素? 我想添加鼠标效果,如放大 - Elementor WordPress - How to highlight all elements with the same css class when you hover over an element? I want to add mouse effect like Zoom In - Elementor WordPress 将所有子元素悬停在Jquery上 - Hover all child elements with Jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM