• textarea高度自适应


    function autoTextarea (ele, maxHeight) {
        var elem = document.getElementById(ele)
        // html自身的字体大小
        var baseFontSize = parseFloat(document.documentElement.style.fontSize)
        maxHeight = maxHeight * baseFontSize
        // 是否是火狐浏览器
        var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window;
        var isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera');
    
        /**
         * 添加时事件
         * @param type 事件名称
         * @param callback 触发后的回调
         */
        function addEvent (type, callback) {
          elem.addEventListener ? elem.addEventListener(type, callback, false) : elem.attachEvent('on' + type, callback);
        }
    
        /**
         * 获取元素的样式
         * @param name 待获取的样式名称
         * @returns {*}
         */
        function getStyle (name) {
          if (elem.currentStyle) {
            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;
          } else {
            return getComputedStyle(elem, null)[name];
          }
        }
    
        var minHeight = parseFloat(getStyle('height'));
    
        elem.style.resize = 'none';
    
        /**
         * 修改元素高度
         */
        function change () {
          var scrollTop
          var height
          var padding = 0
          var 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 + '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();
      }
    

      做了下移动端的自适应,源代码转载自:https://www.cnblogs.com/leshao/p/6851897.html 

  • 相关阅读:
    JTAG的SWD接线方式
    Qt のEXecl
    人脸识别
    Qt实现基本QMainWindow主窗口程序
    Qt学习之路MainWindow学习过程中的知识点
    QT_FORWARD_DECLARE_CLASS
    标准的并发控制实现
    C++ DFS
    C# 互操作(一) 编写一个C++ COM组件
    Socket使用SOAP调用WCF
  • 原文地址:https://www.cnblogs.com/wqc5730/p/10050364.html
Copyright © 2020-2023  润新知