其他分享
首页 > 其他分享> > textarea高度自适应

textarea高度自适应

作者:互联网

源代码:

html

            <textarea id="rea" placeholder="说点什么吧" ></textarea>

js

<script>
    function makeExpandingArea(el) {
        var timer = null;
        //由于ie8有溢出堆栈问题,故调整了这里
        var setStyle = function(el, auto) {
            if (auto) el.style.height = 'auto';
            el.style.height = el.scrollHeight + 'px';
        }
        var delayedResize = function(el) {
            if (timer) {
                clearTimeout(timer);
                timer = null;
            }
            timer = setTimeout(function() {
                setStyle(el)
            }, 200);
        }
        if (el.addEventListener) {
            el.addEventListener('input', function() {
                setStyle(el, 1);
            }, false);
            setStyle(el)
        } else if (el.attachEvent) {
            el.attachEvent('onpropertychange', function() {
                setStyle(el)
            })
            setStyle(el)
        }
        if (window.VBArray && window.addEventListener) { //IE9
            el.attachEvent("onkeydown", function() {
                var key = window.event.keyCode;
                if (key == 8 || key == 46) delayedResize(el);

            });
            el.attachEvent("oncut", function() {
                delayedResize(el);
            }); //处理粘贴
        }
    }
    var textarea = document.getElementById('rea');
    makeExpandingArea(textarea);
</script>

 

标签:function,el,textarea,高度,timer,setStyle,适应,attachEvent,var
来源: https://www.cnblogs.com/wanlibingfeng/p/11151145.html