$('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
});
});
以下守则来自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);
答案 0 :(得分:2)
有一个新版本的backgroundPosition插件至少可以与IE8一起使用:
http://plugins.jquery.com/project/backgroundPosition-Effect
http://plugins.jquery.com/files/jquery.backgroundPosition.js_6.txt
演示:http://jsbin.com/evizu(可通过http://jsbin.com/evizu/edit进行编辑)