JS实现表格一列宽度拖拽

Code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.   
  4. <head>  
  5. <script language="javascript" src="../JS/jquery-1.3.2-vsdoc2.js"></script>  
  6.     <title>ResizeTable</title>  
  7.     <style type="text/css">  
  8.         td, th   
  9.         {   
  10.             height: 20px;   
  11.             border: solid 1px black;   
  12.         }   
  13.     body   
  14.     {   
  15.         overflow:scroll;   
  16.     }   
  17.     </style>  
  18.   
  19.     <script type="text/javascript">  
  20.   
  21.         function getoffset(src) {   
  22.             var ct = 0; //获取控件相对于顶部的位置   
  23.             var cl = 0; //获取控件相对于左边的位置   
  24.             var p = src;   
  25.             while (p.offsetParent) {   
  26.                 ct += p.offsetTop;   
  27.                 cl += p.offsetLeft;   
  28.                 pp = p.offsetParent;   
  29.             }   
  30.             return [ct, cl, src.clientWidth, src.clientHeight]   
  31.         }   
  32.         function enter(src, e) {   
  33.   
  34.             if (!document.down) {   
  35.                 document.resizeControl = src;   
  36.             }   
  37.         }   
  38.   
  39. //        $(document).ready(function() {   
  40. //        $("th").mouseover(function() {   
  41. //        $("th").style.cursor = "W-resize";   
  42. //            });   
  43. //        } );   
  44.         //鼠标松开实现方法,设置图标为箭头,清空一些全局变量。重置document.down = null  
  45.         document.onmouseup = function() {   
  46.             document.down = null;   
  47.             var src = document.resizeControl;   
  48.             if (src) {   
  49.                 src.style.cursor=null;   
  50.                 src.resize = null;   
  51.                 src.srcData = null;   
  52.                 document.resizeControl = null;   
  53.   
  54.             }   
  55.         }   
  56.         //鼠标点击方法,   
  57.         document.onmousedown = function(e) {   
  58.             document.down = 1;   
  59.             if (document.resizeControl) {   
  60.                 var src = document.resizeControl;   
  61.                 var srcData = getoffset(src);   
  62.                 var eev = e || event;   
  63.                 var cX = ev.clientX;   
  64.                 var cY = ev.clientY;   
  65.                 if (cY >= srcData[0] && cY <= srcData[0] + srcData[3] && cX >= srcData[1] + srcData[2] - 10 && cX <= srcData[1] + srcData[2]) {   
  66.                     src.srcData = [src.clientWidth, ev.clientX];   
  67.                     src.resize = 1;   
  68.                 }   
  69.             }   
  70.         }   
  71.         //鼠标移动方法   
  72.         document.onmousemove = function(e) {   
  73.             if (document.resizeControl) {   
  74.                 var src = document.resizeControl;   
  75.                 var srcData = getoffset(src);   
  76.                 var eev = e || event;   
  77.                 var cX = ev.clientX;   
  78.                 var cY = ev.clientY;   
  79.                 //如果可以扩充,则改变鼠标样子。   
  80.                 if (cY >= srcData[0] && cY <= srcData[0] + srcData[3] && cX >= srcData[1] + srcData[2] - 10 && cX <= srcData[1] + srcData[2]) {   
  81.                    
  82.                     src.style.cursor = "W-resize";   
  83.                 }   
  84.                 else {   
  85.                     if (!document.down) {   
  86.                         src.style.cursor = null;   
  87.                     }   
  88.                 }   
  89.                 if (document.down && src.resize) {   
  90.                     // 自身长度+增量   
  91.                     //var newWidth = src.srcData[0] + cX - src.srcData[1];   
  92.                     var newWidth =  src.srcData[0] +cX - src.srcData[1];   
  93.                     if(newWidth > 0)   
  94.                     {   
  95.                     src.style.width= (newWidth).toString() + "px";   
  96.                     }   
  97.                 }   
  98.             }   
  99.         }   
  100.     </script>  
  101.   
  102. </head>  
  103. <body>  
  104.     <table style="table-layout: auto;" cellspacing="0" cellpadding="0" rules="all">  
  105.         <tr>  
  106.             <th onclick="enter(this);" style="width: 100px;">  
  107.                 序号   
  108.             </th>  
  109.             <th onclick="enter(this);" style="width: 100px;">  
  110.                 物料编码   
  111.             </th>  
  112.             <th onclick="enter(this);" style="width: 100px;">  
  113.                 零配件名称   
  114.             </th>  
  115.             <th onclick="enter(this);" style="width: 100px;">  
  116.                 规格型号   
  117.             </th>  
  118.             <th onclick="enter(this);" style="width: 100px;">  
  119.                 图号   
  120.             </th>  
  121.             <th onclick="enter(this);" style="width: 100px;">  
  122.                 计量单位   
  123.             </th>  
  124.             <th onclick="enter(this);" style="width: 100px;">  
  125.                 数量   
  126.             </th>  
  127.             <th onclick="enter(this);" style="width: 100px;">  
  128.                 金额   
  129.             </th>  
  130.         </tr>  
  131.         <tr>  
  132.             <td>  
  133.                 1   
  134.             </td>  
  135.             <td>  
  136.             </td>  
  137.             <td>  
  138.                 减震座   
  139.             </td>  
  140.             <td>  
  141.                 6406   
  142.             </td>  
  143.             <td>  
  144.             </td>  
  145.             <td>  
  146.                 件   
  147.             </td>  
  148.             <td>  
  149.                 10   
  150.             </td>  
  151.             <td>  
  152.                 30   
  153.             </td>  
  154.         </tr>  
  155.         <tr>  
  156.             <td>  
  157.                 2   
  158.             </td>  
  159.             <td>  
  160.             </td>  
  161.             <td>  
  162.                 压簧   
  163.             </td>  
  164.             <td>  
  165.                 13045045   
  166.             </td>  
  167.             <td>  
  168.             </td>  
  169.             <td>  
  170.                 件   
  171.             </td>  
  172.             <td>  
  173.                 20   
  174.             </td>  
  175.             <td>  
  176.                 143.9   
  177.             </td>  
  178.         </tr>  
  179.         <tr>  
  180.             <td>  
  181.                 3   
  182.             </td>  
  183.             <td>  
  184.             </td>  
  185.             <td>  
  186.                 销   
  187.             </td>  
  188.             <td>  
  189.                 14006010   
  190.             </td>  
  191.             <td>  
  192.             </td>  
  193.             <td>  
  194.                 件   
  195.             </td>  
  196.             <td>  
  197.                 4   
  198.             </td>  
  199.             <td>  
  200.                 196   
  201.             </td>  
  202.         </tr>  
  203.         <tr>  
  204.             <td>  
  205.                 4   
  206.             </td>  
  207.             <td>  
  208.             </td>  
  209.             <td>  
  210.                 销   
  211.             </td>  
  212.             <td>  
  213.                 2519705006   
  214.             </td>  
  215.             <td>  
  216.             </td>  
  217.             <td>  
  218.                 件   
  219.             </td>  
  220.             <td>  
  221.                 2   
  222.             </td>  
  223.             <td>  
  224.                 330   
  225.             </td>  
  226.         </tr>  
  227.         <tr>  
  228.             <td>  
  229.                 5   
  230.             </td>  
  231.             <td>  
  232.             </td>  
  233.             <td>  
  234.                 销   
  235.             </td>  
  236.             <td>  
  237.                 2519740119   
  238.             </td>  
  239.             <td>  
  240.             </td>  
  241.             <td>  
  242.                 件   
  243.             </td>  
  244.             <td>  
  245.                 2   
  246.             </td>  
  247.             <td>  
  248.                 90   
  249.             </td>  
  250.         </tr>  
  251.         <tr>  
  252.             <td>  
  253.                 6   
  254.             </td>  
  255.             <td>  
  256.             </td>  
  257.             <td>  
  258.                 压簧   
  259.             </td>  
  260.             <td>  
  261.                 0.5*7*13   
  262.             </td>  
  263.             <td>  
  264.             </td>  
  265.             <td>  
  266.                 件   
  267.             </td>  
  268.             <td>  
  269.                 2   
  270.             </td>  
  271.             <td>  
  272.                 1.66   
  273.             </td>  
  274.         </tr>  
  275.         <tr>  
  276.             <td>  
  277.                 7   
  278.             </td>  
  279.             <td>  
  280.             </td>  
  281.             <td>  
  282.                 滚轮   
  283.             </td>  
  284.             <td>  
  285.                 0012001032   
  286.             </td>  
  287.             <td>  
  288.             </td>  
  289.             <td>  
  290.                 件   
  291.             </td>  
  292.             <td>  
  293.                 10   
  294.             </td>  
  295.             <td>  
  296.                 1170   
  297.             </td>  
  298.         </tr>  
  299.         <tr>  
  300.             <td>  
  301.                 8   
  302.             </td>  
  303.             <td>  
  304.             </td>  
  305.             <td>  
  306.                 弹簧   
  307.             </td>  
  308.             <td>  
  309.                 0014015085   
  310.             </td>  
  311.             <td>  
  312.             </td>  
  313.             <td>  
  314.                 件   
  315.             </td>  
  316.             <td>  
  317.                 10   
  318.             </td>  
  319.             <td>  
  320.                 100   
  321.             </td>  
  322.         </tr>  
  323.         <tr>  
  324.             <td>  
  325.                 9   
  326.             </td>  
  327.             <td>  
  328.             </td>  
  329.             <td>  
  330.                 销   
  331.             </td>  
  332.             <td>  
  333.                 0033010039   
  334.             </td>  
  335.             <td>  
  336.             </td>  
  337.             <td>  
  338.                 件   
  339.             </td>  
  340.             <td>  
  341.                 10   
  342.             </td>  
  343.             <td>  
  344.                 680   
  345.             </td>  
  346.         </tr>  
  347.         <tr>  
  348.             <td>  
  349.                 10   
  350.             </td>  
  351.             <td>  
  352.             </td>  
  353.             <td>  
  354.                 衬套   
  355.             </td>  
  356.             <td>  
  357.                 0035010014   
  358.             </td>  
  359.             <td>  
  360.             </td>  
  361.             <td>  
  362.                 件   
  363.             </td>  
  364.             <td>  
  365.                 4   
  366.             </td>  
  367.             <td>  
  368.                 828   
  369.             </td>  
  370.         </tr>  
  371.         <tr>  
  372.             <td>  
  373.                 11   
  374.             </td>  
  375.             <td>  
  376.             </td>  
  377.             <td>  
  378.                 衬套   
  379.             </td>  
  380.             <td>  
  381.                 0053013045   
  382.             </td>  
  383.             <td>  
  384.             </td>  
  385.             <td>  
  386.                 件   
  387.             </td>  
  388.             <td>  
  389.                 10   
  390.             </td>  
  391.             <td>  
  392.                 180   
  393.             </td>  
  394.         </tr>  
  395.         <tr>  
  396.             <td>  
  397.                 12   
  398.             </td>  
  399.             <td>  
  400.             </td>  
  401.             <td>  
  402.                 销   
  403.             </td>  
  404.             <td>  
  405.                 0059002028   
  406.             </td>  
  407.             <td>  
  408.             </td>  
  409.             <td>  
  410.                 件   
  411.             </td>  
  412.             <td>  
  413.                 5   
  414.             </td>  
  415.             <td>  
  416.                 382   
  417.             </td>  
  418.         </tr>        
  419.     </table>  
  420. </body>  
  421. </html>  

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页