Javascript fadeIn/fadeOut "stacking" - javascript

Trying to get a simple fadeIn/fadeOut work the way I want, basically, my problem is that it won't overwrite, or stop the function, if I've hovered over another element that triggers the action, and it kind puts it in a queue, and will play all the animations, even after my mouse is not even near the elements. I would like it to not let the function trigger again, unless the fadeout has been finished.
$("p").hover(
function()
{
$(document.getElementById('Bottom_Menu')).fadeIn(200);
},
function()
{
$(document.getElementById('Bottom_Menu')).fadeOut(350);
});

To quickly answer your question, you can use stop().
$("#Bottom_Menu").stop().fadeTo(200, 1);
You don't have to use document.getElementById; instead, just use #id. For classes, use .class. It's all built in to jQuery. :)
UPDATE
I'm now using fadeTo instead of fadeIn, because fadeIn only works when display is none. So if we're canceling the previous animation with stop(), we need to use fadeTo, since the display may not be none. (When you use fadeOut, it fades out the element, and when complete, it sets the element's display to none, which hides the element.)
Notes:
The second property of fadeTo is opacity.
fadeTo, like fadeIn, still automatically changes the display property so the element is visible.

$("#Bottom_Menu").clearQueue();
clears all pending animations/operations to do on this element

Related

How can I reproduce JQuery's finish() method in JQuery UI? [duplicate]

I have a page that uses
$(id).show("highlight", {}, 2000);
to highlight an element when I start a ajax request, that might fail so that I want to use something like
$(id).show("highlight", {color: "#FF0000"}, 2000);
in the error handler. The problem is that if the first highlight haven't finished, the second is placed in a queue and wont run until the first is ready. Hence the question: Can I somehow stop the first effect?
I listed this as a comment for the accepted answer, but I thought it would be a good idea to post it as a standalone answer as it seems to be helping some people having problems with .stop()
FYI - I was looking for this answer as well (trying to stop a Pulsate Effect), but I did have a .stop() in my code.
After reviewing the docs, I needed .stop(true, true)
From the jQuery docs:
http://docs.jquery.com/Effects/stop
Stop the currently-running animation on the matched elements....
When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. If, for instance, an element is being hidden with .slideUp() when .stop() is called, the element will now still be displayed, but will be a fraction of its previous height. Callback functions are not called.
If more than one animation method is called on the same element, the later animations are placed in the effects queue for the element. These animations will not begin until the first one completes. When .stop() is called, the next animation in the queue begins immediately. If the clearQueue parameter is provided with a value of true, then the rest of the animations in the queue are removed and never run.
If the jumpToEnd argument is provided with a value of true, the current animation stops, but the element is immediately given its target values for each CSS property. In our above .slideUp() example, the element would be immediately hidden. The callback function is then immediately called, if provided...
.stop(true,true) will freeze the effect so if it's invisible at the time then it remains invisible. This could be a problem if you are using the pulsate effect.
$('#identifier').effect("pulsate", {times:5}, 1000);
To get around this I added
$('#identifier').stop(true, true).effect("pulsate", { times: 1 }, 1);
In my case, using below code does not work and keep your opacity value remain:
$('#identifier').stop(true, true).effect("pulsate", { times: 1 }, 1);
For me just remove opacity are working:
$('#identifier').stop(true, true).css('opacity','');

Check when an animation has ended in SVG

I am starting an animation in SVG and when it ends I want to know so that I can execute another function. I've tried with endEvent="endAnimate()" but nothing happend. It's possible to check when an animation has ended and if so, can I call another function?
According to the documentation, animation elements have an onend event that you can use.
animation event attribute
An animation event attribute is an event attribute that specifies script to run for a particular animation-related event. See Animation event attributes. The animation event attributes are ‘onbegin’, ‘onend’, ‘onload’ and ‘onrepeat’.
So, if you really want to attach it to an animation element you can use it like onend="endAnimate()"

How to stop previous dom element animation in jquery

I am trying to use animation using jquery. When i mouseover the element, it increases the height and when mouseout, returns to original height.
The Problem is when i mouseover from one element to another element, the previous element's animation is still performing. how to stop the animation of the previous element?
Fiddle code:
http://jsfiddle.net/EVZVQ/
Use .stop(): http://api.jquery.com/stop
$('.div_1').mouseover(function(){
$(this).stop().animate({'height':'200px'},2000);
}).mouseout(function(){
$(this).stop().animate({'height':'100px'},2000);
});
Notice you can chain the event binding functions instead of selecting .div_1 twice in a row.
Here is a demo: http://jsfiddle.net/EVZVQ/1/
When .stop() is called on an element, the currently-running animation
(if any) is immediately stopped. If, for instance, an element is being
hidden with .slideUp() when .stop() is called, the element will now
still be displayed, but will be a fraction of its previous height.
Callback functions are not called.
Source: http://api.jquery.com/stop
Update
You can stop all the divs at once like this (but I'm not sure this is the effect you're looking for):
var $divs = $('.div_1');
$divs.mouseover(function(){
$divs.stop().filter(this).animate({'height':'200px'},250);
}).mouseout(function(){
$divs.stop().filter(this).animate({'height':'100px'},250);
});
Here is a demo: http://jsfiddle.net/EVZVQ/2/
This works better.
$('.div_1').mouseover(function(){
$(this).stop(true).animate({'height':'200px'},200);
});
$('.div_1').mouseout(function(){
$(this).stop(true).animate({'height':'100px'},200);
});
http://jsfiddle.net/diode/EVZVQ/7/

Automatically hide a div using jQuery if it gets no MouseOver in a time interval

I have a workaround that shows a div when a menu item gets MouseOver, but hides both only when the div gets MouseOut. This is required for now.
However, if the div never gets MouseOver, it never hides. What I would like to do is automatically hide the div if it doesn't get MouseOver within a few milliseconds of showing.
The hiding after a time shouldn't be hard, but reseting this on a MouseOver challenges me. Any ideas?
Do something like:
function hideYourDiv() {
$("div#yourdiv").hide();
}
var theTimeout = setTimeout(hideYourDiv, 1000);
$("div#yourdiv").mouseover(function() {
clearTimeout(theTimeout);
});
edited to conform to "proper" use of setTimeout :P (though for simple stuff like this I sometimes prefer to pass it a string.. both work anyway)
You can use setTimeout() to set a timeout, but you can also use clearTimeout. Each time you get a mouseOver, you can clear the previous timeOut and set a new one.

How do I stop an effect in jQuery

I have a page that uses
$(id).show("highlight", {}, 2000);
to highlight an element when I start a ajax request, that might fail so that I want to use something like
$(id).show("highlight", {color: "#FF0000"}, 2000);
in the error handler. The problem is that if the first highlight haven't finished, the second is placed in a queue and wont run until the first is ready. Hence the question: Can I somehow stop the first effect?
I listed this as a comment for the accepted answer, but I thought it would be a good idea to post it as a standalone answer as it seems to be helping some people having problems with .stop()
FYI - I was looking for this answer as well (trying to stop a Pulsate Effect), but I did have a .stop() in my code.
After reviewing the docs, I needed .stop(true, true)
From the jQuery docs:
http://docs.jquery.com/Effects/stop
Stop the currently-running animation on the matched elements....
When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. If, for instance, an element is being hidden with .slideUp() when .stop() is called, the element will now still be displayed, but will be a fraction of its previous height. Callback functions are not called.
If more than one animation method is called on the same element, the later animations are placed in the effects queue for the element. These animations will not begin until the first one completes. When .stop() is called, the next animation in the queue begins immediately. If the clearQueue parameter is provided with a value of true, then the rest of the animations in the queue are removed and never run.
If the jumpToEnd argument is provided with a value of true, the current animation stops, but the element is immediately given its target values for each CSS property. In our above .slideUp() example, the element would be immediately hidden. The callback function is then immediately called, if provided...
.stop(true,true) will freeze the effect so if it's invisible at the time then it remains invisible. This could be a problem if you are using the pulsate effect.
$('#identifier').effect("pulsate", {times:5}, 1000);
To get around this I added
$('#identifier').stop(true, true).effect("pulsate", { times: 1 }, 1);
In my case, using below code does not work and keep your opacity value remain:
$('#identifier').stop(true, true).effect("pulsate", { times: 1 }, 1);
For me just remove opacity are working:
$('#identifier').stop(true, true).css('opacity','');

Categories