Allow a textarea resize to shrink (not just grow) element from its original/default size

荒凉一梦 提交于 2019-12-10 04:00:20

问题


I am trying to create a textarea element that has a programmatically-defined original/default size, but which the user can resize within defined limits that allow it to both grow and shrink.

I have tried setting the resize, min-width, max-width, and width properties of the text-area, but, thus far, this only allows the user to grow the textarea element, not shrink it. The browser will allow the user to size and resize the element within the bounds of width and max-width, not between the bounds of min-width and max-width, as intended.

Is there a simple way of specifying a default size that will not also be treated as a minimum size?


Here is the code I am working with:

function textAreaWithDefault(size) {
    var ta = document.createElement("textArea");

    ta.style.resize = "both";
    ta.style.minWidth = "100px";
    ta.style.width = size + "px";    // default size
    ta.style.maxWidth = "500px";

    document.body.appendChild(ta);
} 

A pure css and/or javascript solution is preferred, but it only has to work on web-kit.


回答1:


I would look into fittext or similar scripts: http://fittextjs.com/

This does exactly what you are describing.




回答2:


Try to set min-width equal to max-width and width to 0



来源:https://stackoverflow.com/questions/13037177/allow-a-textarea-resize-to-shrink-not-just-grow-element-from-its-original-defa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!