2014-02-27 91 views
3

我有一个位于页脚的textarea元素(模拟iOS中消息的本机撰写框)。随着文本输入,它会相应增长或缩小。这个元素的高度似乎计算为52px。jQuery Mobile textarea调整大小问题

我想高度要小一些。但是,设置初始高度(不重要)只会被52px覆盖。将初始高度设置为大约35像素(重要)会禁用自动增长功能,而会出现滚动条。我已经与网络督察玩过,但我似乎无法弄清楚这个52px是从哪里计算出来的。

如何解决这个问题?代码非常简单。我正在使用jQuery 1.10.2和jQuery Mobile 1.4.0。

HTML

<div data-role="footer" data-theme="a" data-position="fixed" data-tap-toggle="false" class="custom-footer"> 
     <div class="group-footer"> 
      <div class="map-icon"> 
       <img src="assets/[email protected]" style="width: 25px;height: 25px;" /> 
      </div> 
      <div class="text-box"> 
       <textarea name="textarea" class="define-textarea" id="inbox-continue-conversation"></textarea> 
      </div> 
      <div class="send-link"><a href="#" id="inbox-continue-answer-button">Send</a> 
      </div> 
     </div> 

</div> 

CSS

.define-textarea { 
font-size: 1em !important; 
border-color: #cccccc !important; 
border-radius: 5px; 
} 

Height is 52px. Ideally, I would like it to be around 35px. The auto-grow feature should look like this.

+0

你能提供一个jsfiddle吗? – marionebl

+0

不幸的是,我无法在jsfiddle上复制此行为@marionebl – geekchic

+0

无论如何你能发布吗?差异通常会有所帮助,而其他代码也可以提供提示。 – marionebl

回答

6

您的问题从jQuery Mobile的源this line茎。在计算自动调整jquery mobile textarea窗体小部件的新高度时,它会将15px添加到计算出的高度。

这是硬编码,所以没有选项来调整这里。

你可以用textinput小部件的猴子补丁来解决这个问题。它必须做两件事:

  1. 为增加的高度添加默认选项,例如, autogrowSpace
  2. 覆盖私有函数_updateHeight$.mobile.widgets.textinput在使用这个新选项

工作的jsfiddle采用讨论猴补丁

http://jsfiddle.net/marionebl/48c7x/2/

_updateHeightjQuery mobile source复制:

(function ($, undefined) { 

    $.widget("mobile.textinput", $.mobile.textinput, { 
     options: { 
      autogrow:true, 
      autogrowSpace: 0, 
      keyupTimeoutBuffer: 100 
     }, 

     _updateHeight: function() { 
      var paddingTop, paddingBottom, paddingHeight, scrollHeight, clientHeight, 
      borderTop, borderBottom, borderHeight, height, 
      scrollTop = this.window.scrollTop(); 
      this.keyupTimeout = 0; 

      // IE8 textareas have the onpage property - others do not 
      if (!("onpage" in this.element[0])) { 
       this.element.css({ 
        "height": 0, 
         "min-height": 0, 
         "max-height": 0 
       }); 
      } 

      scrollHeight = this.element[0].scrollHeight; 
      clientHeight = this.element[0].clientHeight; 
      borderTop = parseFloat(this.element.css("border-top-width")); 
      borderBottom = parseFloat(this.element.css("border-bottom-width")); 
      borderHeight = borderTop + borderBottom; 
      height = scrollHeight + borderHeight + this.options.autogrowSpace; 

      if (clientHeight === 0) { 
       paddingTop = parseFloat(this.element.css("padding-top")); 
       paddingBottom = parseFloat(this.element.css("padding-bottom")); 
       paddingHeight = paddingTop + paddingBottom; 

       height += paddingHeight; 
      } 

      this.element.css({ 
       "height": height, 
        "min-height": "", 
        "max-height": "" 
      }); 

      this.window.scrollTop(scrollTop); 
     }, 

    }); 

})(jQuery); 
+0

很棒的发现!我从来没有想过要看那里,但他们硬编码的15px似乎有点不可思议。任何想法为什么他们会?但除此之外,非常感谢您解释得很好的解决方案! – geekchic

+2

我猜想增加的高度是为了确保'textarea'和'input'之间的明显区别。为什么这个用户体验决定强加于每个人,没有一个简单的方法来覆盖它 - 我想根本没有人想到类似于你的用例。但是,然后jQuery手机是OS,并且可以通过GitHub为此发布拉取请求。也许我今天晚些时候会做这件事,然后会更新我的答案。 – marionebl

+0

+1在我看来,这是一个很好的答案,因为您提供了一个覆盖默认行为的解决方案! –