jQuery Mobile 文本区域调整大小问题

标签 jquery css jquery-mobile

我有一个位于页面页脚的文本区域元素(模拟 iOS 中消息的 native 撰写框)。输入文本时,它会相应地增大或缩小。这个元素的高度似乎被计算为 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;
}

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

最佳答案

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

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

您可以使用文本输入小部件的猴子补丁来解决这个问题。它必须做两件事:

  1. 为增加的高度添加默认选项,例如自动增长空间
  2. 覆盖 $.mobile.widgets.textinput 中的私有(private)函数 _updateHeight 以使用此新选项

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

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

示例 _updateHeight 复制自jQuery 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);

关于jQuery Mobile 文本区域调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063680/

相关文章:

javascript - 如何覆盖 ajax=false 以便它切换到加载的 dom 页面(如果存在)

jquery - 如何获取 jqm rangeslider 和 sliderthumb 之间的标签?

javascript - WordPress 插件,jquery noconflict 问题

javascript - 增加 z-index 和降低不透明度的幻灯片

javascript - jQuery 用户界面 : How can I know that the div are center on drag event

javascript - 更改 iframe src 重定向到主页

javascript - 将元素动画到另一个元素的位置

javascript - 交互式 JavaScript 栏

javascript - jQuery:动画不透明度为 0 然后淡出

javascript - JQuery mobile - 使用转换而不使用changePage