简体   繁体   English

以编程方式调整文本区域的大小,而无需修改最小大小

[英]Resize textarea programmatically without modifying min-size

As you can see here , the textarea won't resize under 100px*100px. 如您在此处看到的,textarea的大小不会在100px * 100px以下。 Apparently, it's the normal behaviour. 显然,这是正常的行为。

But how can I initialize a textarea to a custom size, and then let the user resize it at the size he wants afterwards (bigger AND smaller) without using jQuery ui? 但是,如何在不使用jQuery ui的情况下,将文本区域初始化为自定义大小,然后让用户将其调整为自己想要的大小(更大和更小)呢?

The only way seems to be using the mouse down/move/up events, right? 唯一的方法似乎是使用鼠标下移/上移/上移事件,对吗?

Here is a related question (with no related answers). 是一个相关的问题(没有相关的答案)。

In Chrome, you cant resize a textarea to a size smaller than its original height and width . 在Chrome中,您无法将textarea的大小调整为小于其原始的heightwidth

This is actually specified in the W3C specifications , which disctates browsers can defined such characteristics: 这实际上是在W3C规范中指定的,该规范表明浏览器可以定义以下特征:

The user agent may restrict the resizing range to something suitable, such as between the original formatted size of the element, and large enough to encompass all the element's contents. 用户代理可以将调整大小的范围限制在适当的范围内,例如介于元素的原始格式化大小之间,并且必须足够大以包含所有元素的内容。

That said, this has also been raised as a bug in Chrome 话虽如此,这也被认为是Chrome中错误

You can, however, override this using Javascript, simply give your textarea the class resizable and add the below to your page: 但是,您可以使用Javascript覆盖它,只需为textarea提供resizable的类,然后将以下内容添加到您的页面即可:

Demo Fiddle 演示小提琴

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<len; ++i){
    els[i].onmouseover = resizableStart;
}

Textarea has attribute rows and cols. Textarea具有属性行和列。 Try to define this as per your custom size.. ie 尝试根据您的自定义大小定义它。即

<textarea rows="4" cols="50">
I am 4 rows and 50 columns textarea..
</textarea>

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

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