使用javascript调整html表TD的大小

我有这个HTML表:

<table>
    <tr>
        <td id="c0">bla</td>
        <td class="Resizor" id="c01" onm ousedown="setPosition(event);" onm ouseover="setResizeColumns(&#39;c01&#39;, &#39;c0&#39;, &#39;c1&#39;);">&nbsp;</td>
        <td id="c1">bla</td>
        <td class="Resizor" id="c12" onm ousedown="setPosition(event);" onm ouseover="setResizeColumns(&#39;c12&#39;, &#39;c1&#39;, &#39;c2&#39;);">&nbsp;</td>
        <td id="c2" >bla</td>
    </tr>
    <tr>
        <td>blu</td>
        <td></td>
        <td>blu</td>
        <td></td>
        <td>blu</td>
    </tr>
    <tr>
        <td>blu</td>
        <td></td>
        <td>blu</td>
        <td></td>
        <td>blu</td>
    </tr>
</table>

这是我的js脚本:

var MinSize=0;
var _startPosition = 0;
var _diffPosition = 0;
var _allowMove = false;
var _resizerColumn = null;
var _firstColumn = null;
var _secondColumn = null;
var _resizerColumnLeft = 0;
var _resizerColumnWidth = 0;
var _firstColumnLeft = 0;
var _firstColumnWidth = 0;
var _secondColumnLeft = 0;
var _secondColumnWidth = 0;
var _systemEvent = null;
if (navigator.appName == 'Netscape') {
    document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP | Event.ONLOAD);
}
document.onmouseup = disableMouseMovement;
document.onmousemove = setNewPosition;
function setPosition(e) {
    // Called for onm ouseDown event
    if (navigator.appName == 'Netscape') {
    _systemEvent = e;
    } else {
    _systemEvent = event;
    }
    _startPosition = _systemEvent.clientX;
    _allowMove = true;  
    _resizerColumnLeft = findPosX(_resizerColumn);
    _resizerColumnWidth = _resizerColumn.offsetWidth; //_resizerColumn.style.width;
    _firstColumnLeft = findPosX(_firstColumn);
    _firstColumnWidth = _firstColumn.offsetWidth; //_firstColumn.style.width;
    _secondColumnLeft = findPosX(_secondColumn);
    _secondColumnWidth = _secondColumn.offsetWidth; //_secondColumn.style.width;
    return true;
}

function setResizeColumns(resizerColumn, firstColumn, secondColumn) {
    // Called for onm ouseOver event
    // resizerColumn is the actual object of the column that will be moved so that
    // firstColumn and secondColumn can be resized.
    // firstColumn will have its dimensions either grown or shrunk.
    // secondColumn will have the exact opposite done to it that firstColumn has.
    // If firstColumn is shrink by 60px, secondColumn is grown by 60px, the opposite also holds true.
    resizerColumn=document.getElementById(resizerColumn);
    firstColumn=document.getElementById(firstColumn);
    secondColumn=document.getElementById(secondColumn);
    if (_allowMove == false) {
        _resizerColumn = resizerColumn;
        _firstColumn = firstColumn;
        _secondColumn = secondColumn;
    }
    return true;
}

function disableMouseMovement(e) {
    // Called for onm ouseUp event
    _allowMove = false;
    return false;
}

function setNewPosition(e) {
    // Called for onm ouseMove event
    // BEGIN_HACK so that setPosition() can work.
    if (navigator.appName == 'Netscape') {
    _systemEvent = e;
    } else {
    _systemEvent = event;
    }
    // END_HACK
    newPosition = _systemEvent.clientX;
    if (_allowMove) {
        _diffPosition = _startPosition - newPosition;

        var tpos1 = (parseInt(_firstColumnWidth) - parseInt(_diffPosition)) ;
        var tpos2 = (parseInt(_secondColumnWidth) + parseInt(_diffPosition)) ;
        if (tpos1<MinSize) return;
        if ((tpos2<MinSize) && (_firstColumnWidth>tpos1)) return;
        if (tpos2<0) tpos2=0;
        if (tpos1<0) tpos1=0;

        _firstColumn.style.width = tpos1+ "px";
        _secondColumn.style.width = tpos2+ "px";
    }
    return true;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
    curleft += obj.x;
    return curleft;
}

function findPosY(obj){
    var curtop = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
    curtop += obj.y;
    return curtop;
}

我想调整< Td>超过桌子宽度的100%,并且没有通过添加水平滚动(不会固定到屏幕的100%)来停止调整大小
我的代码的问题是将宽度固定为100%(请参阅演示).有解决方案吗?

LIVE DEMO

解决方法:

需要进行三个更改.

第一次更改-传递参数以确定已选择哪个列
(仅为了清楚起见,删除其他信息)

 <td onm ousedown="setPosition(event,1);" />
 <td onm ousedown="setPosition(event,2);" />

第二次更改-保存已选择的列

function setNewPosition(e,col) {
colSelected = col;
...
}

第三次更改-根据选择的颜色更改宽度

if(colSelected == 1)
 _firstColumn.style.width = tpos1+ "px";
else if(colSelected == 2)
 _secondColumn.style.width = tpos2+ "px";

更新:
试试这个-
     _firstColumn.style.width = tpos1“ px”;
     //_secondColumn.style.width = tpos2“ px”;

虽然我建议您检查这些问题-Resizable table columns with bz code

还有下面的jsFiddle
http://jsfiddle.net/tpqn7/

上一篇:如何使用wxpython调整大小并绘制图像?


下一篇:4 django系列之HTML通过form标签来同时提交表单内容与上传文件