最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to stop jquery animation when my page is not active - Stack Overflow

programmeradmin0浏览0评论

I would like to stop my jquery animation when I open new tab in browser. Every time when I leave my page for a while and I e back after some time, my jquery slider goes crazy and it change slides like insane, because I didn't stop it. Do you have some solution?

I would like to stop my jquery animation when I open new tab in browser. Every time when I leave my page for a while and I e back after some time, my jquery slider goes crazy and it change slides like insane, because I didn't stop it. Do you have some solution?

Share Improve this question asked Feb 12, 2012 at 1:40 user794596user794596 531 silver badge7 bronze badges 1
  • Please post a detailed example along with some relevant code. I know you already got your answer but this whole thing will be useless to future readers otherwise. – Sparky Commented Feb 12, 2012 at 2:53
Add a ment  | 

2 Answers 2

Reset to default 8

In JavaScript:

window.addEventListener('focus', function() {
    // code to start 
});
window.addEventListener('blur', function() {
    // code to stop
});

With jQUery:

$(window).bind('focus', function() {
  // code to start
});
$(window).bind('blur', function() {
  // code to stop
});

I read somewhere about this and it's called animation queue buildup. Instead of trying to stop animation when opening new tab, put stop() just before starting an animation.

This will ensure that previous animation is killed before continuing....

for example:

$('.some-selector').stop().slideUp();

Hope this helps...

发布评论

评论列表(0)

  1. 暂无评论