Custom CSS to allow chrome textarea resize smaller than initial state?

后端 未结 3 1368
滥情空心
滥情空心 2020-12-03 15:33

I like Chrome\'s nifty textarea resize control. However, the one exception is that the textarea appears to have a hard min-height/min-width setting that\'s not changeable vi

3条回答
  •  臣服心动
    2020-12-03 15:47

    Another javascript solution:

    Demo: http://jsfiddle.net/nz8ut/2/

    function resizableStart(e){
        this.originalW = this.clientWidth;
        this.originalH = this.clientHeight;
        this.onmousemove = resizableCheck;
        this.onmouseup = this.onmouseout = resizableEnd;
    }
    function resizableCheck(e){
        if(this.clientWidth !== this.originalW || this.clientHeight !== this.originalH) {
            this.originalX = e.clientX;
            this.originalY = e.clientY;
            this.onmousemove = resizableMove;
        }
    }
    function resizableMove(e){
        var newW = this.originalW + e.clientX - this.originalX,
            newH = this.originalH + e.clientY - this.originalY;
        if(newW < this.originalW){
            this.style.width = newW + 'px';
        }
        if(newH < this.originalH){
            this.style.height = newH + 'px';
        }
    }
    function resizableEnd(){
        this.onmousemove = this.onmouseout = this.onmouseup = null;
    }
    
    var els = document.getElementsByClassName('resizable');
    for(var i=0, len=els.length; i

    The solution above uses mouseover and mouseout to trigger resizableStart and resizableEnd. The problem is that if the element being resized has childs, when the mouse is placed over a child, the element receives a mouseout event and, just after that, it receives a mouserover event which bubbles from child.

    To avoid those events I have implemented another solution with mouseenter and mouseleave events:

    Demo: http://jsfiddle.net/nz8ut/3/

提交回复
热议问题