Javascript 发光/脉动效果在点击时停止
我有以下 Javascript 来使文本链接连续发光/脉动。此链接显示同一页面的另一部分,因此我希望它在用户单击它后停止。
<script type="text/javascript">
$(document).ready(function() {
function pulsate() {
$(".pulsate").animate({opacity: 0.2}, 1200, 'linear')
.animate({opacity: 1}, 1200, 'linear', pulsate);
}
pulsate();
});
</script>
所以基本上,我只需要知道需要在此处添加什么,以便单击后效果就会停止。
如果再次点击同一个链接,页面显示的部分就会隐藏——再次点击后再次启动效果是否太麻烦?
我期待你们好心人的答复。
斯科特.
I have the following Javascript to make a text link glow/pulsate continuously. This link reveals another section of the same page so I would like it to stop once the user has clicked on it.
<script type="text/javascript">
$(document).ready(function() {
function pulsate() {
$(".pulsate").animate({opacity: 0.2}, 1200, 'linear')
.animate({opacity: 1}, 1200, 'linear', pulsate);
}
pulsate();
});
</script>
So basically, I just need to know what I need to add here so that the effect stops once it has been clicked.
If the same link is clicked again, the revealed section of the page will hide - is it too much trouble to make the effect start again after a second click?
I look forward to an answer from you good people.
Scott.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需绑定到点击事件并调用 stop() 即可。您还应该确保不透明度已恢复为 1:
这是一个工作的 jsFiddle。
Simply bind to the click event and call stop(). You should also ensure that the opacity has been restored to 1:
Here's a working jsFiddle.
解决方案非常简单。让您的
pulsate()
函数在执行其操作之前确保.pulsate
不具有类stop
。如果它确实具有该类,则 pulsate() 函数将简单地将链接动画化回完全不透明,但不会继续脉动。James 的示例也有效,但我更喜欢我的方法,因为他的方法一遍又一遍地将
click
事件绑定到.pulsate
。这种事情可能会导致问题,具体取决于页面的其余部分正在做什么。实例:http://jsfiddle.net/2f9ZU/
The solution is pretty simple. Have your
pulsate()
function make sure that.pulsate
doesn't have the classstop
before doing its thing. If it does have that class, then thepulsate()
function will simply animate the link back to full opacity, but not continue the pulsating.James' example works as well, but I prefer my approach because his way binds the
click
event to.pulsate
over and over again. This kind of thing may cause problems depending on what the rest of your page is doing.Live example: http://jsfiddle.net/2f9ZU/