简体   繁体   English

用鼠标调整表格列的大小

[英]Resize table columns with mouse

I am writing a script to allow a client to mouse drag table cell borders and resize columns in a table. 我正在编写一个脚本,允许客户端鼠标拖动表格单元格边框并调整表格中的列大小。 So far I have a working model in Firefox but there is a flaw in width measurement that leaves the mouse out of sync when the change gets large. 到目前为止,我在Firefox中有一个可行的模型,但是宽度测量存在一个缺陷,当更改变大时,鼠标将不同步。 Worse, the script fails in other browsers (opera,safari) or even if I change the browser zoom in Firefox. 更糟糕的是,该脚本在其他浏览器(opera,safari)中失败,或者即使我在Firefox中更改了浏览器缩放比例,该脚本也无法运行。

function doDrag() {document.body.style.cursor='crosshair';}

function noDrag() {document.body.style.cursor='auto';}

var xpos=0;
var sz=0;
var dragObj = {};
function resizeOn(el) 
    {
    dragObj = document.getElementById(el);
    document.addEventListener("mousemove",resize, true);
    document.addEventListener("mouseup",resizeOff, true);
    }

function resize(ev)
    {
    if(xpos == 0) {xpos=ev.clientX;}
    if(xpos != ev.clientX) 
        {
        sz = dragObj.offsetWidth + (ev.clientX - xpos);
        dragObj.style.width = sz - 10 + "px";
        alert("size="+sz+" offsetwidth="+dragObj.offsetWidth);
        if(dragObj.offsetWidth != sz)
            {
            resizeOff();
            return false;
            }
        xpos=ev.clientX;
        }
    }

function resizeOff() 
    {
    xpos = 0;
    document.removeEventListener("mousemove",resize, true);
    document.removeEventListener("mouseup",resizeOff, true);
    }

The HTML looks like: HTML看起来像:

<th id="col0" class="edit">client</th>
<th class="drag" onmouseover="doDrag()" onmouseout="noDrag()" onmousedown="resizeOn('col0')"></th>

The second cell is made to appear as the right edge of the first. 使第二个单元格显示为第一个单元格的右边缘。

I assume the problem is dragObj.style.width = sz - 10. The -10 was derived purely by trial and error. 我认为问题是dragObj.style.width = sz-10。-10纯粹是通过反复试验得出的。 I suspect this is the difference between the actual width of the cell including borders, padding etc and offsetwidth. 我怀疑这是单元格的实际宽度(包括边框,填充等)和offsetwidth之间的差异。 It should really be, per my css, 10 for padding + 1 for the left border = 11px. 根据我的CSS,实际上应该是10填充+ 1左边框= 11px。 Either my fixed padding/borders aren't staying fixed or there is some other css property between the offsetWidth and the actual with of the element. 我固定的填充/边框未保持固定,或者offsetWidth与元素的实际with之间存在其他一些CSS属性。 Is there some way to get the actual width of the element regardless of the browsers scaling? 有什么方法可以获取元素的实际宽度,而不管浏览器如何缩放?

Have a look at the documentation in www.quirksmode.org and/or http://help.dottoro.com . 请访问www.quirksmode.org和/或http://help.dottoro.com中的文档。 Confirm whether the properties are supported by your target browser. 确认目标浏览器是否支持这些属性。 They also have comments on how zoom affects offsetX and similar. 他们还对缩放如何影响offsetX和类似内容发表了评论。

Also, you should note that ev.clientX has been broken in IE by a recent patch (KB2846071). 另外,您应该注意,ev.clientX已在IE中被最新补丁(KB2846071)破坏。 If the patch is installed, clientX returns a meaningless result. 如果安装了补丁,则clientX返回无意义的结果。 Hopefully MS will release a patch for their patch! 希望MS会为其补丁发布补丁!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM