2014-02-27 92 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在我看來,這是一個很好的答案,因爲您提供了一個覆蓋默認行爲的解決方案! –