Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I created a small slider where I set classes with JS every X seconds and do the animation with CSS Transition.

Now when the window is not active (for example you watch at another tab) and you come back there is a bit of chaos. After 1 transition-time-period the chaos is gone. It looks like some animations arnt run while on another window.

It looks like JS is running but not the CSS Transitions while the window is not active.

A simple example: https://jsfiddle.net/ugxkjr3s/

Keep the window inactive for a few seconds and you see the Div move faster. The left is set a few times with the function move. But CSS Transition only kicks in when the window is active again. So the Div moves to the end position all at once.

setInterval(move, 1000);
var left=0;

function move() {
    $("div").css("left", left);
  left=left+20;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
281 views
Welcome To Ask or Share your Answers For Others

1 Answer

The problem is that when the tab/window is hidden, browser doesn't redraw its content but the script continues to run, and when you switch back, it catches up at the first script execution that cause a redraw.

What you could do is use the Page Visibility API and stop script from running when tab/window is non visible


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...