表格鼠标经过有底色的js

来源:互联网 发布:西双版纳房地产数据 编辑:程序博客网 时间:2024/04/28 19:07
用了CSDN ID叫火龙果的网友的js

  1. /**
  2.  * 仿 Prototype 的 $ 方法
  3.  */
  4. window.$ = function(id) {
  5.   if(typeof id == 'string') {
  6.     return document.getElementById(id);
  7.   }
  8.   return id;
  9. }
  10. /**
  11.  * 色彩渐变
  12.  * @param start   起始颜色,使用 # 颜色表
  13.  * @param end     终止颜色
  14.  * @param step    渐变步长,大于 1 并且小于 200,默认为 16
  15.  */
  16. var ColorGradient = function(start, end, step) {
  17.   this.start = this._toRGB(start);
  18.   this.end = this._toRGB(end);
  19.   this.step = (function() {
  20.       if(!step || step < 1 || step > 200) {
  21.         return 16;
  22.       }
  23.       return step;
  24.     })();
  25.   this.gradient = [];
  26. }
  27. ColorGradient.prototype = {
  28.   // 获得渐变的颜色数组,以 6 位十六进制字符串返回
  29.   getGradient : function() {
  30.     return this._getGradient();
  31.   },
  32.   // 获得所设定的步长值
  33.   getStep : function() {
  34.     return this.step;
  35.   },
  36.   // 测试工作,用于在页面上渐变测试
  37.   test : function() {
  38.     var grad = this._getGradient();
  39.     var n = 400 / this.step;
  40.     var p = document.createElement('div');
  41.     for(var i = 0; i < grad.length; i++) {
  42.       var d = document.createElement('div');
  43.       d.className = 'grad';
  44.       d.style.width = n + 'px';
  45.       d.style.backgroundColor = '#' + grad[i];
  46.       p.appendChild(d);
  47.     }
  48.     document.body.appendChild(p);
  49.   },
  50.   // 生成渐变色数组
  51.   _getGradient : function() {
  52.     if(this.gradient.length > 0)  {
  53.       // 如果渐变色变量中有值直接返回
  54.       return this.gradienth;
  55.     }
  56.     // 生成各步的颜色
  57.     for(var i = 0; i <= this.step; i++) {
  58.       var color = this._getNewColor(i).toString(16);
  59.       while(color.length < 6) {
  60.         color = '0' + color;
  61.       }
  62.       this.gradient.push(color);
  63.     }
  64.     return this.gradient;
  65.   },
  66.   // 根据 RGB 变化量获得每步的 RGB 颜色
  67.   _getNewColor : function(k) {
  68.     var rgbStep = this._getStepRgb();
  69.     var rgb = 0;
  70.     for(var i = 0; i < 3; i++) {
  71.       var c = Math.floor(this.start[i] + rgbStep[i] * k);
  72.       rgb |= c << ((3 - i - 1) * 8);
  73.     }
  74.     return rgb;
  75.   },
  76.   // 获得 RGB 三种颜色的变化量
  77.   _getStepRgb : function() {
  78.     var rgb = [];
  79.     // 依次获得 R、G、B 的变化步长
  80.     for(var i = 0; i < 3; i++) {
  81.       rgb.push(this._getStepVar(i));
  82.     }
  83.     return rgb;
  84.   },
  85.   // 根据渐变步长、起始、终止颜色获得步长的变化量
  86.   _getStepVar : function(index) {
  87.     return (this.end[index] - this.start[index]) / this.step;
  88.   },
  89.   // 将 # 颜色转换成 RGB 颜色数组
  90.   // 索引 0 -- 红色 R
  91.   // 索引 1 -- 绿色 G
  92.   // 索引 2 -- 蓝色 B
  93.   _toRGB : function(color) {
  94.     var v = color.substring(1);
  95.     var c = parseInt(v, 16);
  96.     var rgb = [];
  97.     for(var i = 0; i < 3; i++) {
  98.       rgb.push(c >> ((3 - i - 1) * 8) & 0xff);
  99.     }
  100.     return rgb;
  101.   }
  102. }
  103. /**
  104.  * 事件处理工具类
  105.  */
  106. var Event = function(){}
  107. Event = {
  108.   /**
  109.    * 为 element 使用 handler 处理程序添加至 event 事件
  110.    * 兼容 IE 及 Firefox 等浏览器
  111.    *
  112.    * 例如为 botton 对象添加 onclick 事件,使用 clickEvent
  113.    * 方法作为处理程序:
  114.    *   Event.addEvent(botton, 'click', clickEvent);
  115.    *
  116.    * @param element  需要添加事件的对象(Object)
  117.    * @param event    需要添加的事件名称(String),不加“on”
  118.    * @param handler  需要添加的方法引用(Function)
  119.    */
  120.   addEvent : function(element, event, handler) {
  121.     if(element.attachEvent) {
  122.       element.attachEvent('on' + event, handler);
  123.     } else if (element.addEventListener) {
  124.       element.addEventListener(event, handler, false);
  125.     } else {
  126.       element['on' + event] = handler;
  127.     }
  128.   },
  129.   /**
  130.    * 添加事件处理程序时,只能添加一个方法的引用,并不能给
  131.    * 方法加上参数。比如定义了 clickEvent(str) 这个方法,现
  132.    * 在要将其作为 obj 的 onclick 的事件处理程序,就可以用:
  133.    * obj.onclick = Event.getFuntion(null, clickEvent, str);
  134.    */
  135.   getFunction : function(obj, fun) {
  136.     var args = [];
  137.     obj = obj || window;
  138.     for(var i = 2; i < arguments.length; i++) {
  139.       args.push(arguments[i]);
  140.     }
  141.     return function() {
  142.         fun.apply(obj, args);
  143.       };
  144.   }
  145. }
  146. var Table = function(id, time, start, end) {
  147.   this.rows   = (function() {
  148.       var tab = $(id);
  149.       if(tab.tBodies.length == 0) {
  150.         return tab.rows;
  151.       }
  152.       var rows = [];
  153.       for(var i = 0; i < tab.tBodies.length; i++) {
  154.         for(var j = 0; j < tab.tBodies[i].rows.length; j++) {
  155.           rows.push(tab.tBodies[i].rows[j]);
  156.         }
  157.       }
  158.       return rows;
  159.     })();
  160.   this.start  = start || Table.DEFAULT_START;
  161.   this.end    = end || Table.DEFAULT_END;
  162.   this.time   = time || Table.DEFAULT_TIME;
  163.   var color = new ColorGradient(this.start, this.end, 16);
  164.   this.grad   = color.getGradient();
  165.   this.step   = color.getStep();
  166. }
  167. Table.DEFAULT_TIME = 10;
  168. Table.DEFAULT_START = '#dcdcdc';
  169. Table.DEFAULT_END = '#ffffff';
  170. Table.prototype = {
  171.   addEvent : function() {
  172.     for(var i = 0; i < this.rows.length; i++) {
  173.       this.rows[i].style.backgroundColor = this.end;
  174.       Event.addEvent(this.rows[i], 'mouseover',
  175.           Event.getFunction(thisthis.overEvent, this.rows[i]));
  176.       Event.addEvent(this.rows[i], 'mouseout',
  177.           Event.getFunction(thisthis.outEvent, this.rows[i]));
  178.     }
  179.   },
  180.   overEvent : function(row) {
  181.     row.style.backgroundColor = this.start;
  182.     row.change = this.step;
  183.   },
  184.   outEvent : function(row) {
  185.     var me = this;
  186.     if(row.change-- > 0) {
  187.       row.style.backgroundColor = '#' + me.grad[me.step - row.change];
  188.       setTimeout(function() {
  189.           me.outEvent(row);
  190.         }, this.time);
  191.     }
  192.   },
  193.   _getRows : function(id) {
  194.     var tab = $(id);
  195.     alert(tab.tBodies.length);
  196.   }
  197. }
保存为 tableboder.js

然后新建一个html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
  5.   <title>Table</title>
  6.   <script language="javascript" src="tableboder.js"></script>
  7.   <script type="text/javascript">
  8.     /**
  9.     *
  10.     *调用方法就是下面,用表格id把table new出来,加上事件。
  11.     */
  12.     window.onload = function() {
  13.       var tab1 = new Table('tab1');
  14.       tab1.addEvent();
  15.     }
  16.     </script>
  17. </head>
  18. <style type="text/css">
  19. div.grad {
  20.   float: left;
  21.   height: 100px;
  22.   margin: 0;
  23.   padding: 0;
  24. }
  25. table {
  26.   border-top: 1px solid black;
  27.   border-left: 1px solid black;
  28.   border-spacing: 0;
  29.   font-family: 宋体,新宋体;
  30. }
  31. table td {
  32.   border-bottom: 1px solid black;
  33.   border-right: 1px solid black;
  34.   padding: 2px 5px;
  35. }
  36. </style>
  37. <body>
  38. <table id='tab1' width="60%" align="center">
  39.     <tr>
  40.         <td>1</td>
  41.         <td>2</td>
  42.         <td>3</td>
  43.         <td>4</td>
  44.     </tr>
  45.     <tr>
  46.         <td>5</td>
  47.         <td>6</td>
  48.         <td>7</td>
  49.         <td>8</td>
  50.     </tr>
  51.     <tr>
  52.         <td>1</td>
  53.         <td>2</td>
  54.         <td>3</td>
  55.         <td>4</td>
  56.     </tr>
  57.     <tr>
  58.         <td>5</td>
  59.         <td>6</td>
  60.         <td>7</td>
  61.         <td>8</td>
  62.     </tr>
  63.     <tr>
  64.         <td>1</td>
  65.         <td>2</td>
  66.         <td>3</td>
  67.         <td>4</td>
  68.     </tr>
  69.     <tr>
  70.         <td>5</td>
  71.         <td>6</td>
  72.         <td>7</td>
  73.         <td>8</td>
  74.     </tr>
  75. </table>
  76. </body>
  77. </html>
下面是个非常需要注意的小问题

引入外部js时,<script language="javascript" src="tableBoder.js"></script>必须写language 属性,而不能使type属性。
原创粉丝点击