totalnum_animateBackground-plugin.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function($) {
  2. if(!document.defaultView || !document.defaultView.getComputedStyle){
  3. var oldCurCSS = jQuery.curCSS;
  4. jQuery.curCSS = function(elem, name, force){
  5. if(name === 'background-position'){
  6. name = 'backgroundPosition';
  7. }
  8. if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
  9. return oldCurCSS.apply(this, arguments);
  10. }
  11. var style = elem.style;
  12. if ( !force && style && style[ name ] ){
  13. return style[ name ];
  14. }
  15. return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
  16. };
  17. }
  18. var oldAnim = $.fn.animate;
  19. $.fn.animate = function(prop){
  20. if('background-position' in prop){
  21. prop.backgroundPosition = prop['background-position'];
  22. delete prop['background-position'];
  23. }
  24. if('backgroundPosition' in prop){
  25. prop.backgroundPosition = '('+ prop.backgroundPosition + ')';
  26. }
  27. return oldAnim.apply(this, arguments);
  28. };
  29. function toArray(strg){
  30. strg = strg.replace(/left|top/g,'0px');
  31. strg = strg.replace(/right|bottom/g,'100%');
  32. strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
  33. var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
  34. return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
  35. }
  36. $.fx.step.backgroundPosition = function(fx) {
  37. if (!fx.bgPosReady) {
  38. var start = $.curCSS(fx.elem,'backgroundPosition');
  39. if(!start){//FF2 no inline-style fallback
  40. start = '0px 0px';
  41. }
  42. start = toArray(start);
  43. fx.start = [start[0],start[2]];
  44. var end = toArray(fx.end);
  45. fx.end = [end[0],end[2]];
  46. fx.unit = [end[1],end[3]];
  47. fx.bgPosReady = true;
  48. }
  49. var nowPosX = [];
  50. nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
  51. nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
  52. fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
  53. };
  54. })(jQuery);