Py学习  »  Jquery

如何向jQuery.addClass和.removeClass添加延迟?

twelvell • 4 年前 • 908 次点击  

我需要加一个 延迟 200ms 在这里:

jQuery('.class').hover(() => jQuery('#custom').addClass('addedclass'), () => jQuery('#custom').removeClass('addedclass'));

jQuery: Can I call delay() between addClass() and such? 但那里的结构略有不同。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54339
 
908 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Phong
Reply   •   1 楼
Phong    4 年前

你可以用 setTimeout 去实现它。

jQuery('.class').hover(
() => setTimeout(() => jQuery('#custom').addClass('addedclass'), 200), 

() => setTimeout(() => jQuery('#custom').removeClass('addedclass'), 200));
.addedclass{
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="class">
  <p id="custom">Text</p>
</div>