js为table列宽度添加拖拽调整

 


//var table = document.getElementById("table1");
//addResize($(".layui-table")[0]);

/** Table添加列宽度调整 */
function addResize(table) {

    var td;
    var mousedown = function (event) {
        td = this;
        if (event.offsetX > td.offsetWidth - 10) {
            td.mouseDown = true;
            td.screenX0 = event.screenX;
            td.oldWidth = td.offsetWidth;
        }
        //console.log("onmousedown():" + event.screenX);
    };

    var mouseup = function (event) {
        if (td == undefined) td = this;
        td.mouseDown = false;
        td.style.cursor = 'default';
        //console.log("onmouseup():" + event.screenX);
    };

    var mousemove = function (event) {

        //更改鼠标样式
        if (event.offsetX > this.offsetWidth - 10)
            this.style.cursor = 'col-resize';
        else
            this.style.cursor = 'default';

        //调整宽度
        if (td == undefined) td = this;
        if (td.mouseDown != null && td.mouseDown == true) {

            if ((event.screenX - td.screenX0) != 0) {
                td.style.cursor = 'col-resize';

                td.width = td.oldWidth + (event.screenX - td.screenX0);
                td.style.width = td.width;
                //table.rows[0].cells[td.cellIndex].style.width = td.width
                //console.log("onmousemove():" + event.screenX);
            }
        }
    };

    // 修改表头列宽度信息	
    var cells = table.rows[0].cells;
    for (j = 0; j < cells.length; j++) {
        var cell = cells[j];
        cell.onmousedown = mousedown;
        cell.onmouseup = mouseup;
        cell.onmousemove = mousemove;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值