美文网首页
蜜汁textarea

蜜汁textarea

作者: _YDS | 来源:发表于2017-07-29 21:27 被阅读0次

使textarea可以达到具有默认提示文字,且当用户输入文字时提示文字自动删除,并且由提示文字的字体颜色浅灰色变为正常的输入文字黑色,可通过js实现。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>title</title>
    
    <style>
        #textarea{
             overflow:hidden;
            margin-top:2rem;
            margin-left:0.5rem;
            height:10rem;
            width:95%;
            outline: none;
            border:0;
            background:#f9f9f9;
        }
    </style>
</head>
<body>
        <textarea  id="textarea" placeholder="添加作品描述,如:塔米APP的iOS主开发工程师,负责整体架构设计..."></textarea>  
        
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    apiready = function(){
        
    };
    var autoTextarea = function (elem, extra, maxHeight) {
        extra = extra || 0;
        var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
        isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
                addEvent = function (type, callback) {
                        elem.addEventListener ?
                                elem.addEventListener(type, callback, false) :
                                elem.attachEvent('on' + type, callback);
                },
                getStyle = elem.currentStyle ? function (name) {
                        var val = elem.currentStyle[name];
                        if (name === 'height' && val.search(/px/i) !== 1) {
                                var rect = elem.getBoundingClientRect();
                                return rect.bottom - rect.top -
                                        parseFloat(getStyle('paddingTop')) -
                                        parseFloat(getStyle('paddingBottom')) + 'px';        
                        };
                return val;
                } : function (name) {
                return getComputedStyle(elem, null)[name];
                },
                minHeight = parseFloat(getStyle('height'));
                elem.style.resize = 'none';
                var change = function () {
                var scrollTop, height,
                        padding = 0,
                        style = elem.style;
 
                if (elem._length === elem.value.length) return;
                elem._length = elem.value.length;
 
                if (!isFirefox && !isOpera) {
                        padding = parseInt(getStyle('paddingTop')) + parseInt(getStyle('paddingBottom'));
                };
                scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
 
                elem.style.height = minHeight + 'px';
                if (elem.scrollHeight > minHeight) {
                        if (maxHeight && elem.scrollHeight > maxHeight) {
                                height = maxHeight - padding;
                                style.overflowY = 'auto';
                        } else {
                                height = elem.scrollHeight - padding;
                                style.overflowY = 'hidden';
                        };
                        style.height = height + extra + 'px';
                        scrollTop += parseInt(style.height) - elem.currHeight;
                        document.body.scrollTop = scrollTop;
                        document.documentElement.scrollTop = scrollTop;
                        elem.currHeight = parseInt(style.height);
                };
        };
        addEvent('propertychange', change);
        addEvent('input', change);
        addEvent('focus', change);
        change();
};

</script>
<script type="text/javascript">
    var text=document.getElementById("textarea");
    autoTextarea(text);//调用
</script>
</html>
</html>

相关文章

  • 蜜汁textarea

    使textarea可以达到具有默认提示文字,且当用户输入文字时提示文字自动删除,并且由提示文字的字体颜色浅灰色变为...

  • js 实现一键复制

    const textArea = document.createElement('textarea'); ...

  • JS使用textarea模拟post提交表单

    textarea 标签 textarea代表HTML表单多行输入域textarea标签是成对出现的,以 开始,以 ...

  • textarea自动生成空格怎么解决?

    textarea自动生成空格是因为 textarea 标签之间有换行或空格导致的。 textarea元素中...

  • 小程序 底部 textarea ios 错误完美解决