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

Why this example does not work in IE http://jsfiddle.net/8RZVt/

I get this error in IE8.

Message: Invalid argument. Line: 156 Char: 295 Code: 0 URI: http://code.jquery.com/jquery-1.4.4.min.js 
+6
javascript jquery css cross-browser xhtml
source share
4 answers

According to jQuery, this is because, as stated on the animate documentation animate :

All animated properties must have the same numerical value (except as noted below); properties that are not numeric cannot be animated using the basic jQuery functionality ....

So, in fact, in Firefox you are using undefined behavior. The right thing to do is 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

Upon closer inspection, the plugin does not support += or -= formats.

I hacked it in this example:

http://jsfiddle.net/CxqSs/ (see the new example below.)

It can definitely use some cleanup and should probably be added to this plugin, but it works in both browsers and does not rely on undefined behavior.

By the way, I don’t know whether it’s worth noting, but if you leave this animation long, it will eventually overflow the value and break. This can be overcome by animating the entire length of the background image, and then resetting the offset to 0px in the callback until the next animation. This would also avoid the += format.

AND

It should be noted that speed: 1, step: 1 and speed: 50, step: 50 equivalent.

The reason they look at different speeds is because

  • Overhead exceeds 1 (which is actually a millisecond), because animate is more often called.
  • The default easing is “rocking,” which means the animation speeds up and slows down a bit throughout the course, which means that the overall speed is slightly changed. You must change the attenuation to “linear” for your scroll case:

     var animate = function() { element.animate({ ... }, speed, "linear", animate); }; 

This means that you can use the backgroundPosition-Effect plugin without "+ =", setting your step to 2247 (image width), as I said above.

And what finally brings us ... wait for him ...

http://jsfiddle.net/zyQj3/20/

Cross-platform, non-kludgy, not crowded, properly weakening, requiring no additional parameters, solution.

+7
source share

The script fails because you are passing an invalid CSS value:

 element.animate({ backgroundPosition: animStep + " 0px" /* evaluates to "+=50px 0px" */ }, speed, animate); 
+4
source share

OK, we go again: D

http://jsfiddle.net/c7rKV/1/

Again, identical to the original, but again just backgroundPositionX animation when in IE.

Sorry to not really watch FF / Chrome one last time.

Additionally: this, of course, is not very elegant, and Adam Prax is absolutely right that the problem. I just wanted to post a solution.

+2
source share

If you check the jQuery source code, you will see that it uses this regexp to parse the parameter (which in your case is +=50px 0px ). Therefore, it will look like += (increase) 50 (up to fifty) px 0px (unit, added after the number). When trying to read the current value, jQuery uses parseFloat , which simply captures the number at the beginning of the line. This way it works just fine, even if the multidimensional property is probably not what jQuery programmers had in mind.

Except that IE8 does not support getting the current background-position value. There is background-position-x and background-position-y , but not background-position . Duh. Therefore, it is best to check the browser type and animate either background-position or background-position-x , depending on this: http://jsfiddle.net/22UWW/

(Actually there is a jQuery bug report about this, with a more elegant solution .)

+2
source share

All Articles