How to stop animation in Dojo - javascript

Using Dojo 1.9, I'm playing some animations like this:
that.fadeOutActive = baseFx.fadeOut({ node: "active-container", duration: 1000, delay: 3000 });
that.fadeInInactive = baseFx.fadeIn({ node: "inactive-container", duration: 1000, delay: 3000 });
coreFx.combine([that.fadeOutActive, that.fadeInInactive]).play();
and then try to stop them on an event, like this:
coreFx.combine([that.fadeOutActive, that.fadeInInactive]).stop();
The problem is, this prevents the animation from firing (which is the desired behavior), but it doesn't stop it if it had already begun (which is the problem). How can I stop the animation, if I can at all?
EDIT: It turns out my problem was not in the code I've posted, it was in detecting if the animation is in progress.

dojo/fx::combine() is a helper function that can take a list of dojo/_base/fx::Animation objects and combine them so that their effects all run in parallel. With this function animations that affect multiple nodes can be generated and executed at the same time.
You are using coreFx.combine to simultaneously fadeIn one element and fadOut another element.
Instead of creating a new combined Animation you should store a reference to the existing combined animation and then use that reference to stop the combined animation:
that.fadeOutActive = baseFx.fadeOut({ node: "active-container", duration: 1000, delay: 3000 }).play();
that.fadeInInactive = baseFx.fadeIn({ node: "inactive-container", duration: 1000, delay: 3000 }).play();
that.combinedAnim = coreFx.combine([that.fadeOutActive, that.fadeInInactive]).play();
and then later:
that.combinedAnim.stop();

Related

Fading in a Javascript Object after a certain Time

So I have particle object in JavaScript which is called after a certain Time simply like this:
setTimeout(function() {
particle();
}, 3000);
Now I want this object to be faded in slowly to the canvas after the 3 seconds but I don't know how. I have tried using the jQuery fadeIn() function but it did nothing to the object.
EDIT: I am using the library particles.js from https://vincentgarreau.com/particles.js/ if you want to see the whole logic behind particle();. You can find a working code snippet there. I have not changed the code by a lot except for the styling of the particles.
Thanks a lot
i would do it with jquery the following way. my way of doing it requires jquery easing plugin. you hve to grap the corresponding DOM-element and do some animation on it.
$(document).ready(function () {
var headline = $("#app-title");
// Do time out here
headline.css({'opacity': 0});
headline.show();
headline.animate({opacity: 1}, {
step: function (now, mx) {
headline.css({'opacity': opacity});
},
duration: 800,
complete: function () {
},
//this comes from the custom easing plugin
easing: 'easeInOutBack'
});
});

Trigger script on scroll

jQuery({someValue: 0}).animate({someValue: 38}, {
duration: 1500, easing:'swing',
step: function() {
$('#grow1').text(Math.ceil(this.someValue));
}
});
How can i make sure the animation doesn't start until the user scrolls down to a particular div?
You can use something like http://scrollmagic.io. It allows you to define some triggers in the page and then execute callbacks (in this case your animation) when those triggers are reached by the user. Take a look at this example: http://scrollmagic.io/examples/basic/custom_actions.html
Hope this will help you

Jquery Cycle changing options on the fly

I want to change the options for jquery cycle dynamically on the page. Specifically, I want the speed to drop. According to one of it's creator, you can use 'cycle.opts' to do this. In my example I have specifically
$('.cycle-streams').cycle({
fx: 'scrollVert',
continuous: 1,
speed: 1000,
delay: 0,
easing: 'linear'
});
var changedOpts = $('.cycle-streams').data('cycle.opts');
$('.cycle-streams').mouseover(function() {
var changedOpts = $('.cycle-streams').data('cycle.opts');
changedOpts.speed = 1000000000000;
$('.cycle-streams').data('cycle.opts', changedOpts);
});
I've been working on this for some time now and am lost as to what I'm doing wrong. Any help would be appreciated. The jsfiddle is here... http://jsfiddle.net/bmXgj/
I changed your fiddle to this:
var changedOpts = $('.cycle-streams').data('cycle.opts');
$('.cycle-streams').mouseover(function() {
//hover in
$('.cycle-streams').cycle('pause');
changedOpts.speedIn = 500;
changedOpts.speedOut = 500;
$('.cycle-streams').cycle('next');
$('.cycle-streams').cycle('resume');
});
$('.cycle-streams').mouseout(function() {
//hover out
$('.cycle-streams').cycle('pause');
changedOpts.speedIn = 3000;
changedOpts.speedOut = 3000;
//$('.cycle-streams').cycle('next');
$('.cycle-streams').cycle('resume');
});​
I noticed that you were re-declaring 'changedOpts' for some reason. When I watched this variable in the console, it was coming out as undefined. Since you already defined it I just kept using it.
also you were never resetting the state of the cycle. I added pause and resume. It appears to work now but it is not perfect. The last side needs to clear before the new speeds will be used, so drastic jumps (3000 to 500 for example) allow for a noticeable delay when trying to speed up. I also added a next statement.
I noticed that when the plugin was handling next it was clearing the timeouts. So I figured if I stored the remaining time (Pause), then moved to the next slide (next), then resumed the show (resume) that the time would be restored. This seems to be working.
The 'next' action, is not needed when going from fast to slow because you will not usually notice the 'orphan' slide when leaving the container because it is cycling quickly. I put it in there for good measure in case your speeds are not that far apart.
Also I noticed you were only changing one speed. I am assuming, without looking at the guts of the plugin, that the 'speed' option is used at initialization only. After adding the other two speeds I got the right action. After reviewing the 'guts' of the plugin, I am certain that the speed option is used for synchronizing the other two speeds. If they all do not match you may get out of sync. The initialization code uses the 'speed' to set 'speedIn' and 'speedOut', so we need to change those two to affect the 'running' speed.
http://jsfiddle.net/bmXgj/6/
EDIT: did not need to pause and resume.
EDIT2: I did need the pause and resume, but I also needed a next. Also the 'speed' option can be ignored once it is set the first time to start the cycle. It appears to be shorthand for speedIn = speedOut = X

setTimeout speeds up with multiple tabs

I’m having a setTimeout problem similar to this one. But that solution doesn't help me since I can’t use php in my file.
My site has a slider with a list of images that move every 8 seconds.However, when I have opened a few tabs in the browser and then switch back again, it goes nuts.
The slider proceeds to move the images one after the other immediately without the 8 second timedelay.
I'm only seeing it in Chrome and the latest Firefox.
**EDIT: I checked with console.log() and the setTimeout returns the same number before and after the clearTimeout. Not sure why. Maybe that also has something to do with it? **
EDIT 2: I added a fiddle: http://jsfiddle.net/Rembrand/qHGAq/8/
The code looks something like:
spotlight: {
i: 0,
timeOutSpotlight: null,
init: function()
{
$('#spotlight .controls a').click(function(e) {
// do stuff here to count and move images
// Don't follow the link
e.preventDefault();
// Clear timeout
clearTimeout(spotlight.timeOutSpotlight);
// Some stuff here to calculate next item
// Call next spotlight in 8 seconds
spotlight.timeOutSpotlight = setTimeout(function () {
spotlight.animate(spotlight.i);
}, 8000);
});
// Select first item
$('#spotlight .controls a.next:first').trigger('click');
},
animate: function(i)
{
$('#spotlight .controls li:eq(' + (spotlight.i) + ') a.next').trigger('click');
}
}
From the jQuery documentation:
Because of the nature of requestAnimationFrame(), you should never
queue animations using a setInterval or setTimeout loop. In order to
preserve CPU resources, browsers that support requestAnimationFrame
will not update animations when the window/tab is not displayed. If
you continue to queue animations via setInterval or setTimeout while
animation is paused, all of the queued animations will begin playing
when the window/tab regains focus. To avoid this potential problem,
use the callback of your last animation in the loop, or append a
function to the elements .queue() to set the timeout to start the next
animation.
I finally found my answer and it’s not at all what I was expecting.
It seems the culprit is jQuery’s .animate(), which I use to move the images in the slider.
I calculate and move my images positions with this:
$('.spotlight-inner')
.animate(
{ left: scrollToVal },
{duration: 'slow'}
)
;
Now the problem seems to be that in some browsers, after you switch to a new tab and back, jQuery’s .animate() saves up the animations and fires them all at once. So I added a filter to prevent queueing. That solutions comes from CSS-Tricks.com :
$('.spotlight-inner')
.filter(':not(:animated)')
.animate(
{ left: scrollToVal },
{duration: 'slow'}
)
;
The first slide you see when you go back can act a little jumpy but it’s better than the superspeed carousel from before.
Fiddle with the full code here
There is an easier way using the jquery animate queue property:
$(this).animate({
left: '+=100'
}, {duration:500, queue:false});
I don't know if this will help you, but it helped me with my slideshow. What I did was everytime I called an animation that was supposed to happen at a set interval because of the setTimeout, I called clearQueue() which would get rid of any other animations that had been set to happen. then i'd call the animation. That way when you come back to that tab, you don't have all these animations queued up and it goes crazy. at max you'll only have one set up.
So something like this:
spotlight.timeOutSpotlight = setTimeout(function () {
spotlight.clearQueue(); // get rid of other instances of the animation
spotlight.animate(spotlight.i);
}, 8000);
It may not work in all cases (depending on timing), but I hope that helps somebody!
You must also think you use clearTimeout.
As you call setTimeout function it returns an ID you can save this ID in a variable like
timeoutID = setTimeout(function () {
spotlight.animate(spotlight.i);
}, 8000);
and before setting a new timeout you can call the function like
clearTimeout(timeoutID)
My suspicion is that the browser queues input events like 'click' but only fires them when the tab where the event occurs actually has focus.
Perhaps you should try calling your click callbacks directly instead of using trigger('click').
Something like this:
spotlight: {
i: 0,
timeOutSpotlight: null,
clickFunc: function(element) {
// do stuff here to count and move images
// Clear timeout
clearTimeout(spotlight.timeOutSpotlight);
// Some stuff here to calculate next item
// Call next spotlight in 8 seconds
spotlight.timeOutSpotlight = setTimeout(function () {
spotlight.animate(spotlight.i);
}, 8000);
},
init: function()
{
$('#spotlight .controls a').click(function (e) {
// Don't follow the link
e.preventDefault();
spotlight.clickFunc(this);
});
// Select first item
spotlight.clickFunc($('#spotlight .controls a.next:first'));
},
animate: function(i)
{
var element = $('#spotlight .controls li:eq('+spotlight.i+') a.next');
spotlight.clickFunc(element);
}
}
What version of jQuery are you running? Apparently this problem was 'fixed' for version 1.6.3 - they reverted the change that caused this to happen. Discussions here and here.
Though this issue will likely have to be addressed in the future, it seems as though we're off the hook for now.

Mootools - FX.Scroll won't stop when another event is fired

I don't have a very good understanding of Javascript so appologies before we start.
I have successfully used Mootools 1.1 to scroll to elements onclick events. I used FX.Scroll as the example here http://demos111.mootools.net/Fx.Scroll and basicly ripped off the demo code.
Note: If you click on one link and then another quickly it immediately stops moving to the first element and scrolls to the second.
I am now trying to use Mootools 1.3 to use the fade efects for a gallery and have used More Builder to get FX.Scroll. It is working BUT when I click on one link and then another straight away, it just continues with the first scroll.
It appears that event.stop is not working.
See example http://www.mytimephotography.co.uk < works
http://www.mytimephotography.co.uk/test < broken
I am using code:
window.addEvent('domready', function () {
var scroll = new Fx.Scroll('scrollcontainer', {
wait: false,
duration: 2000,
offset: {'x': 0, 'y': 0},
transition: Fx.Transitions.Quad.easeInOut
})
$('link1').addEvent ('click', function(event){
event = new Event(event).stop();
scroll.toElement('c1');
})
//etc
})
Please view any other source code on the site.
Use the "link" property of the Fx options object. The default is set to "ignore", which is why the original animation keeps running. Instead, use "chain" if you want it to run after the current animation, or "cancel" if you want it to interrupt the currently running animation.
Alternately, use a faster animation—two seconds is really long! :)
var scroll = new Fx.Scroll('scrollcontainer', {
wait: false,
duration: 2000,
offset: {'x': 0, 'y': 0},
transition: Fx.Transitions.Quad.easeInOut,
link: 'cancel'
});

Categories