Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

javascript - JS / CSS Transition when Window not active

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...