【问题标题】:jQuery Mobile textarea resizing issuejQuery Mobile textarea 调整大小问题
【发布时间】:2014-04-13 23:51:05
【问题描述】:

我有一个 textarea 元素,它位于页面的页脚(模拟 iOS 中消息的本机撰写框)。输入文本时,它会相应地增长或缩小。该元素的高度似乎计算为 52px。

我希望高度更小。但是,设置初始高度(不重要)只会被 52px 覆盖。将初始高度设置为大约 35px(重要)会禁用自动增长功能,并且会出现一个滚动条。我玩过网络检查器,但我似乎无法弄清楚这个 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/compose-action-arrow@2x.png" 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;
}

【问题讨论】:

  • 你能提供一个jsfiddle吗?
  • 不幸的是,我无法在 jsfiddle @marionebl 上复制此行为
  • 你还能发帖吗?这些差异通常会有所帮助,您的其余代码也可以提供提示。
  • 不确定这是否有帮助,但这里是jsfiddle.net/48c7x
  • 好的,jquery-mobile 在textarea 元素上做了一些疯狂的事情,进一步检查它。文档:api.jquerymobile.com/textinput

标签: jquery css jquery-mobile


【解决方案1】:

您的问题源于 jQuery 移动源中的 this line。在为自动调整大小的 jquery mobile textarea 表单小部件计算新高度时,它会将计算的高度增加 15px。

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

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

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

使用讨论过的猴子补丁的工作 jsfiddle

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

示例jQuery mobile source复制_updateHeight

(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);

【讨论】:

  • 很棒的发现!我从没想过要去看那里,尽管他们硬编码的 15px 似乎有点奇怪。知道他们为什么会这样做吗?但除此之外,非常感谢您提供详细解释的解决方案!
  • 我想增加的高度是为了确保textareainput 之间的明显区别。为什么这个 UX 决定被强加给每个人,却没有简单的方法来覆盖它——我想根本没有人考虑过与你类似的用例。但是 jQuery mobile 是操作系统,因此可以通过 GitHub 发布拉取请求。也许我今天晚些时候会过来做这件事,到时会更新我的答案。
  • +1 在我看来很好的答案,因为您提供了一个解决方案来覆盖默认行为!
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2021-11-26
相关资源
最近更新 更多