Ole Media
Ole Media

Reputation: 1642

jquery animate background postion don't work in IE

hey I have been trying for hours and I don't understand why the following does not work with IE but other works. The problem that I'm having is the the background of the parent link, which is a span with an id assigned, does not animate the background image set via CSS. I get an error that says 'Undefined' is null or is not an object

$('a.inactive').click(function(self) {
        var parent = $(this).parent();
        if (parent.css('background-position-x')) {
            var pos = parseInt(parent.css('background-position-x').split(' ')[0]);
        } else {
            var pos = parseInt(parent.css('background-position').split(' ')[0]);
        };
        $.ajax({
           type: "GET",
           url: "somepage.php",
           data: "id="+parent.attr('id').replace('inactive-',''),
           complete: function(){
             //alert("Data Saved: ");
                if(pos < 0) {
                    parent.animate({backgroundPosition: '(0px 0px)'}, {duration:600});
                } else {
                    parent.animate({backgroundPosition: '(-39px 0px)'}, {duration:600});
                }

                //parent.attr('class',$(this).className.replace(/sprite-hover-(\w+)-off/gi,'active1') );
           },
           async: false
         });
    });

The Code below is from Alexander Farkas

(function(jQuery){
        $.extend($.fx.step,{
            backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
                        }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];          
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
        });
})(jQuery);

Upvotes: 0

Views: 1606

Answers (1)

brianpeiris
brianpeiris

Reputation: 10795

There is a new version of the backgroundPosition plugin that works with IE8 at least:

http://plugins.jquery.com/project/backgroundPosition-Effect
http://plugins.jquery.com/files/jquery.backgroundPosition.js_6.txt

Demo: http://jsbin.com/evizu (Editable via http://jsbin.com/evizu/edit)

Upvotes: 2

Related Questions