JQuery synchronous animate not working - javascript

I have a jquery synchronous animation with window.setInterval
window.setInterval(function(){
$("#pageflip img").stop().animate({width:'+=2px',height:'+=2px'}, 800,null,function(){
$("#pageflip img").stop().animate({width:'-=2px',height:'-=2px'}, 800);
});
},1600)
What happening here when I loose focus from page window (switch tab or open another application, not viewing animation page) callback function is not called, image width and height always increasing (not back to its normal position) as long as I remain in same page window it is working fine. I am newbie in jquery any help please?
Sorry for my bad english.

.animate( properties [, duration ] [, easing ] [, complete ] ).
You have the easing parameter value set to null, it should be left empty or set to linear (more options when using the easing plugin).
Easing :
The remaining parameter of .animate() is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.
Remove the ,null or replace it with Linear/swing, before your callback. By default the easing is set to swing.
Refer to the docs for more details.

Related

JQuery ScrollLeft with easing [duplicate]

This question already has answers here:
TypeError: p.easing[this.easing] is not a function
(12 answers)
Closed 8 years ago.
I am trying to scrollLeft with an easing applied.
It scrolls just fine if I took out the ease
$("#content").animate({
scrollLeft: '+=' + $(window).width()
}, "slow");
But when I try to add an easing to the animation
$("#content").animate({
scrollLeft: '+=' + $(window).width()
}, "slow", "easeInOutBounce");
I get an error that looks like:
TypeError: m.easing[this.easing] is not a function
Any help would be appreciated
easeInOutBounce is not part of the default set of jQuery easings. You’ll need to include jQuery UI’s easing library to have access to this. Either just include the whole of jQuery UI, or build yourself a custom version of the library containing just the easing components. Include this after including jQuery and before your code.
You have to add 'easeInOutBounce' to jquery easing array, otherwise you only can use 'linear' or 'swing'.
There are plugins to add easing curves to jquery, jquery-ui adds many of them including 'easeInOutBounce'
I think you need the JQuery UI to have access to this kind of easing.
Source (JQuery documentation):
Easing
The remaining parameter of .animate() is a string naming an easing function to use. An easing function specifies the speed at which the animation progresses at different points within the animation. The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a constant pace, called linear. More easing functions are available with the use of plug-ins, most notably the jQuery UI suite.

remove and add class with linear easing is not working

Not able to control the linear easing in remove class as well as add class $("#masthead").removeClass("header_bottom", 700, "linear");
Here is my site http://developermobulous.com link you can check on scroll the header comes but it goes little above to the default top and little below to the default bottom.
Answer will be really appreciating
$.removeClass is not an animating function. You should only use $("#masthead").removeClass("header_bottom");
You do not have any reference of jquery-ui in your site. You have to include that to work with .removeClass( className [, duration ] [, easing ] [, complete ] ) function. Otherwise its just invoking jquery api .removeClass().

Easing on Fixed Position Jquery

Is there anyway to add a easing to a fixed element?, I've been looking around and I can't find an answer. I really don't know how it would be, maybe something like...
$(window).scroll(function() {
$("#form").animate({position:"fixed", easing: 'swing'});
});
Any help will be appreciated ^ ^ Thanks!
Edit:Pretty much what I'm looking for is when user scrolls, the fixed element obviously will follow the window position, but I want to add is a little delay in comparison to scroll action with an easing effect
You'd have to make that div absolute positioned, z-indexed and without parent, then move it on the scroll event. You can know the number of pixels scrolled with scrollTop(). Something like this:
$(window).scroll(function(){
var offset=100;
//stop is called so easing doesn't affect while it is still scrolling.
$("form").stop().animate({top:($(window).scrollTop()+offset)+"px"}, 300, 'swing');
});
Try this instead:
$("#form").animate({position:"fixed"}, 300, 'swing');
From the jQuery api ( http://api.jquery.com/animate/ ):
The only easing implementations in the jQuery library are the default,
called swing, and one that progresses at a constant pace, called
linear. More easing functions are available with the use of plug-ins,
most notably the jQuery UI suite.
$("form").animate({position:"fixed"}, 300, 'swing'); //swing being default

jQuery example (in jsfiddle) working in firefox but not in IE8, 7

Why this example not working in IE http://jsfiddle.net/8RZVt/
I'm getting this error in IE8.
Message: Invalid argument.
Line: 156
Char: 295
Code: 0
URI: http://code.jquery.com/jquery-1.4.4.min.js
According to jQuery, this is because, as stated on the animate documentation page:
All animated properties should be a
single numeric value (except as noted
below); properties that are
non-numeric cannot be animated using
basic jQuery functionality....
So, in fact, in Firefox you are using undefined behavior. The correct thing to do would be to animate on backgroundPositionX, however Firefox does not support this.
There is, however, a jQuery plugin that does what you want:
http://plugins.jquery.com/project/backgroundPosition-Effect
Update
On closer inspection, the plugin does not support += or -= formats.
I hacked it into this example:
http://jsfiddle.net/CxqSs/ (See new example at bottom.)
Could definitely use some cleanup, and should probably be added to that plug-in, but it works in both browsers and doesn't rely on undefined behavior.
BTW, I don't know if it's worth noting, but if you leave this animation running a long time, it will eventually overflow the value and break. This could be overcome by animating the full length of the background image and then resetting the offset to 0px in the callback before the next animate. This would also avoid needing the += format.
Also,
It should be noted that speed: 1, step: 1 and speed: 50, step: 50 are equivalent.
The reason they look different speeds is because
There is more overhead in a speed of 1 (which is really a millisecond duration) because animate gets called more often.
The default easing is "swing", meaning that the animation speeds up and slows down slightly throughout it's course, meaning that the overall speed is affected a bit. You should change the easing to "linear" for your scrolling case:
var animate = function() {
element.animate({
...
}, speed, "linear", animate);
};
This means that you could use the backgroundPosition-Effect plugin, without the '+=', by setting your step to 2247 (the width of the image), like I stated above.
And that finally brings us to... wait for it...
http://jsfiddle.net/zyQj3/20/
Cross-platform, non-kludgy, non-overflowing, correctly easing, extra parameter-lacking, solution.
The script fails at this point because you are passing an invalid CSS value:
element.animate({
backgroundPosition: animStep + " 0px" /* evaluates to "+=50px 0px" */
}, speed, animate);
OK here we go again :D
http://jsfiddle.net/c7rKV/1/
Again identical to original however again just animating backgroundPositionX when in IE.
Apologies on not actually looking at FF/Chrome last time.
Additionally: this of course is not very graceful and Adam Prax is absolutely correct on what the problem is. I just wanted to post a solution to it.
If you check the source code of jQuery, you will see it uses this regexp to parse the parameter (which in your case is +=50px 0px). So it will see it as += (increase) 50 (to fifty) px 0px (unit, append after the number). When trying to read the current value, jQuery uses parseFloat, which just grabs the number at the start of the string. So it works perfectly, even if a multi-dimensional property is probably not what the jQuery programmers had in mind.
Except that IE8 does not support getting the current value of background-position. There is background-position-x and background-position-y but no background-position. Duh. So your best bet is checking the browser type, and animating either background-position or background-position-x depending on that: http://jsfiddle.net/22UWW/
(There is actually a jQuery bug report about this, with a more elegant solution.)

JQuery animation: Is it possible to change speed during the animation?

I want to move a div down a page, and I want it to slow down as it reaches the target.
I tried using call back with recursive func but it doesn’t look smooth:
function MovePanel() {
sidePanel.animate({
"marginTop": newCurrTop
}, moveSpeed, function () {
MovePanel();
});
}
Is it possible to slow down an JQuery animation?
If not what are the alternatives?
Thanks.
The animate method takes a 3rd param called "easing"; learn about it here:
http://api.jquery.com/animate/
You might want to check this out: http://www.learningjquery.com/2009/02/quick-tip-add-easing-to-your-animations
Easing can really bring life to an
effect. Easing controls how an
animation progresses over time by
manipulating its acceleration.

Categories

Resources