Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2012
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. // main function to process the fade request //
  2. function colorFade(id,element,start,end,steps,speed) {
  3. var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  4. var target = document.getElementById(id);
  5. steps = steps || 20;
  6. speed = speed || 20;
  7. clearInterval(target.timer);
  8. endrgb = colorConv(end);
  9. er = endrgb[0];
  10. eg = endrgb[1];
  11. eb = endrgb[2];
  12. if(!target.r) {
  13. startrgb = colorConv(start);
  14. r = startrgb[0];
  15. g = startrgb[1];
  16. b = startrgb[2];
  17. target.r = r;
  18. target.g = g;
  19. target.b = b;
  20. }
  21. rint = Math.round(Math.abs(target.r-er)/steps);
  22. gint = Math.round(Math.abs(target.g-eg)/steps);
  23. bint = Math.round(Math.abs(target.b-eb)/steps);
  24. if(rint == 0) { rint = 1 }
  25. if(gint == 0) { gint = 1 }
  26. if(bint == 0) { bint = 1 }
  27. target.step = 1;
  28. target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
  29. }
  30.  
  31. // incrementally close the gap between the two colors //
  32. function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
  33. var target = document.getElementById(id);
  34. var color;
  35. if(target.step <= steps) {
  36. var r = target.r;
  37. var g = target.g;
  38. var b = target.b;
  39. if(r >= er) {
  40. r = r - rint;
  41. } else {
  42. r = parseInt(r) + parseInt(rint);
  43. }
  44. if(g >= eg) {
  45. g = g - gint;
  46. } else {
  47. g = parseInt(g) + parseInt(gint);
  48. }
  49. if(b >= eb) {
  50. b = b - bint;
  51. } else {
  52. b = parseInt(b) + parseInt(bint);
  53. }
  54. color = 'rgb(' + r + ',' + g + ',' + b + ')';
  55. if(element == 'background') {
  56. target.style.backgroundColor = color;
  57. } else if(element == 'border') {
  58. target.style.borderColor = color;
  59. } else {
  60. target.style.color = color;
  61. }
  62. target.r = r;
  63. target.g = g;
  64. target.b = b;
  65. target.step = target.step + 1;
  66. } else {
  67. clearInterval(target.timer);
  68. color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
  69. if(element == 'background') {
  70. target.style.backgroundColor = color;
  71. } else if(element == 'border') {
  72. target.style.borderColor = color;
  73. } else {
  74. target.style.color = color;
  75. }
  76. }
  77. }
  78.  
  79. // convert the color to rgb from hex //
  80. function colorConv(color) {
  81. var rgb = [parseInt(color.substring(0,2),16),
  82. parseInt(color.substring(2,4),16),
  83. parseInt(color.substring(4,6),16)];
  84. return rgb;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement