digitalScroll .js 2.1 KB

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