闪烁一个项目。 (Jquery 淡入淡出?)
我有两个 div,我想让它们同时闪烁,直到用户将鼠标悬停在其中一个上。
var shouldiblink = '1';
function mrBlinko(divid){
while (shouldiblink =='1') {
$("#"+divid).fadeIn(100).fadeOut(300);
}
$(document).ready(function(){
mrBlinko("mydiv1");
mrBlinko("mydiv2");
}
我将有一个悬停事件,将 shouldiblink 设置为“0”。问题是,一旦页面准备好并且浏览器崩溃,循环就会开始。
我被这个解决方案困住了,现在想不出替代方案。
你能帮助我吗?
非常感谢。
I have two divs that I want to make blink at the same time until the user hovers the mouse on one of them.
var shouldiblink = '1';
function mrBlinko(divid){
while (shouldiblink =='1') {
$("#"+divid).fadeIn(100).fadeOut(300);
}
$(document).ready(function(){
mrBlinko("mydiv1");
mrBlinko("mydiv2");
}
The I'll have an hover event that sets shouldiblink to '0'. Problem is that the loops starts as soon as the page is ready and the browser crashes.
I'm stuck with this solution and I can't think of an alternative right now.
Can you help me?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为更好的方法是使用 setInterval 和clearInterval。
页面加载后,使用 setInterval 来获得效果。当用户将鼠标悬停在元素上时,然后使用 setInterval 获得的间隔 id 清除间隔。
请参阅工作演示。
I think the better way will be to use setInterval and clearInterval.
Once the page is loaded use setInterval to get the effect going. When the user hovers the mouse over the element then clear the interval using the interval id attained for setInterval.
See a working demo.
替代方案之一 - 来自 jQuery UI 的
Pulsate
效果。从 google API 中包含它以提高性能。
如果您想推出自己的解决方案,您可能会发现查看 脉动效果的源代码。
One of the alternatives -
Pulsate
effect from jQuery UI.Include it from google API in order to improve performance.
If you want to roll your own solution, you might find useful checking out source code of pulsate effect.
尽管我讨厌
标签,但试试这个:
As much as I hated the
<blink>
tag, try this:这是使用 jQuery 完成回调的替代解决方案。
您可以随时将“selected-pulsate”添加到元素并调用 setPulsate(),它将开始脉动。要清除所有当前脉动器,您可以调用clearSelection(),它只会删除该类并强制其隐藏。
clearSelection() [clearTimeout()] 中有一行,我不确定是否有必要。在我极其基本的测试中,它可以在没有那条线的情况下工作,但我不确定计时器是否有可能仍在该点运行并导致问题。
我也不确定在 fadeOut() 完成回调中调用 RepeatCall() 是否会导致堆栈溢出,因此我使用了较小值 10 毫秒的 setTimeout 来再次调用该函数,而不是直接执行。
Here is an alternate solution using the completion callback of jQuery.
You can add 'selected-pulsate' to an element at any time and call setPulsate(), and it will start pulsating. To clear all current pulsators you can call clearSelection() which just removes the class and forces it to be hidden.
There is a line in clearSelection() [clearTimeout()], which I'm not sure is necessary. In my extremely basic testing, it works without that line, but I am not sure whether there is a possibility the timer may still be running at that point and cause issues.
I was also not certain whether calling RepeatCall() within the fadeOut() complete callback would cause a stack overflow, so I used setTimeout with a small value of 10msec to call the function again instead of doing it directly.