admin管理员组文章数量:1304184
I'm going to have cases where I need to pause all the animations on the page in order to do some user interaction, and then resume all the animations as soon as the user has responded.
For example, I might have an animation that's fading in over a 1s period, and 400ms in I need to pause it. It should stop at 40% opacity, and stay there until I resume, at which point it should pick up where it left off and spend another 600ms pleting its fade-in from 40% to 100%.
There might be multiple animations on the page simultaneously, and I want to pause them all. Additionally, some animations might have further animations queued to continue after they plete, and that all needs to work: when I resume, the current animation needs to finish, and then the next one in the queue needs to start, just as if nothing happened.
jQuery doesn't seem to have any built-in support for pausing animations; the closest I can find is .stop()
, which will stop at the current spot in the animation, but can't resume later; and it either moves on to the next animation in the queue immediately, or clears the animation queue entirely.
How can I pause the animations in a way that will let me resume them later?
I'm going to have cases where I need to pause all the animations on the page in order to do some user interaction, and then resume all the animations as soon as the user has responded.
For example, I might have an animation that's fading in over a 1s period, and 400ms in I need to pause it. It should stop at 40% opacity, and stay there until I resume, at which point it should pick up where it left off and spend another 600ms pleting its fade-in from 40% to 100%.
There might be multiple animations on the page simultaneously, and I want to pause them all. Additionally, some animations might have further animations queued to continue after they plete, and that all needs to work: when I resume, the current animation needs to finish, and then the next one in the queue needs to start, just as if nothing happened.
jQuery doesn't seem to have any built-in support for pausing animations; the closest I can find is .stop()
, which will stop at the current spot in the animation, but can't resume later; and it either moves on to the next animation in the queue immediately, or clears the animation queue entirely.
How can I pause the animations in a way that will let me resume them later?
Share Improve this question edited Aug 31, 2017 at 17:09 James Hill 61.9k22 gold badges149 silver badges166 bronze badges asked Mar 8, 2012 at 14:11 Joe WhiteJoe White 97.8k64 gold badges225 silver badges335 bronze badges 1- Never tried it but maybe this is working: archive.plugins.jquery./project/Pause-Resume-animation – m90 Commented Mar 8, 2012 at 14:24
1 Answer
Reset to default 10Pause / Resume Option
The pause plugin will work well for this situation.
HTML
<div class=demo>
<div id="box1" class="animationToPause"></div>
<div id="box2" class="animationToPause"></div>
<div id="box3" class="animationToPause"></div>
</div>
<input id="btnPause" type="button" value="Pause Animations" />
CSS
div.demo {
border: 1px solid #555;
margin: 1em auto;
width: 550px;
}
#box1, #box2, #box3 {
width: 100px;
height: 100px;
position: relative;
}
#box1 {
background: #0C0;
}
#box2 {
background: #090;
}
#box3 {
background: #060;
}
JavaScript
$(function() {
//Begin animation on boxes
$("div[id^='box']").each(function() {
phase1($(this));
});
//Setup animation pause/resume on hover
$("div[id^='box']").hover(function() {
$(this).pause();
}, function() {
$(this).resume();
});
});
//Toggle animation pause/resume on button click
$("#btnPause").toggle(
function() {
$(".animationToPause").pause();
}, function() {
$(".animationToPause").resume()
});
//Animation 1
function phase1($this) {
$this.animate({
left: 450
}, 2000, 'swing', function() {
phase2($this)
});
}
//Animation 2
function phase2($this) {
$this.animate({
left: 0
}, 2000, 'swing', function() {
phase1($this)
});
}
Here's a working fiddle to demonstrate.
Brute Force Option
Certainly not what you're looking for, but related.
Setting jQuery.fx.off = true;
will stop all animations immediately.
本文标签: javascriptHow can I pause all jQuery animationsStack Overflow
版权声明:本文标题:javascript - How can I pause all jQuery animations? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741773322a2396886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论