简体   繁体   English

jQuery自动调整大小功能导致浏览器跳转

[英]jQuery auto resize function causes jumping in browser

I like this text expander because it doesn't lock up the keyboard occasionally like others seem to do as they are calculating the resize. 我喜欢这种文本扩展器,因为它有时不会像其他人在计算调整大小时那样锁定键盘。 However, when I use it the browser sometimes jumps to the top of the page when it resizes (rather than staying put at the point in the page I've scrolled to). 但是,当我使用它时,浏览器有时会在调整大小时跳到页面顶部(而不是停留在我滚动至页面的位置)。 Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗?

/**
 * TextAreaExpander plugin for jQuery
 * v1.0
 * Expands or contracts a textarea height depending on the
 * quatity of content entered by the user in the box.
 *
 * By Craig Buckler, Optimalworks.net
 *
 * As featured on SitePoint.com:
 * http://www.sitepoint.com/blogs/2009/07/29/build-auto-expanding-textarea-1/
 *
 * Please use as you wish at your own risk.
 */

/**
 * Usage:
 *
 * From JavaScript, use:
 *     $(<node>).TextAreaExpander(<minHeight>, <maxHeight>);
 *     where:
 *       <node> is the DOM node selector, e.g. "textarea"
 *       <minHeight> is the minimum textarea height in pixels (optional)
 *       <maxHeight> is the maximum textarea height in pixels (optional)
 *
 * Alternatively, in you HTML:
 *     Assign a class of "expand" to any <textarea> tag.
 *     e.g. <textarea name="textarea1" rows="3" cols="40" class="expand"></textarea>
 *
 *     Or assign a class of "expandMIN-MAX" to set the <textarea> minimum and maximum height.
 *     e.g. <textarea name="textarea1" rows="3" cols="40" class="expand50-200"></textarea>
 *     The textarea will use an appropriate height between 50 and 200 pixels.
 */

(function($) {

    // jQuery plugin definition
    $.fn.TextAreaExpander = function(minHeight, maxHeight) {

        var hCheck = !($.browser.msie || $.browser.opera);

        // resize a textarea
        function ResizeTextarea(e) {

            // event or initialize element?
            e = e.target || e;

            // find content length and box width
            var vlen = e.value.length, ewidth = e.offsetWidth;
            if (vlen != e.valLength || ewidth != e.boxWidth) {

                if (hCheck && (vlen < e.valLength || ewidth != e.boxWidth)) e.style.height = "0px";
                var h = Math.max(e.expandMin, Math.min(e.scrollHeight, e.expandMax));

                e.style.overflow = (e.scrollHeight > h ? "auto" : "hidden");
                e.style.height = h + "px";

                e.valLength = vlen;
                e.boxWidth = ewidth;
            }

            return true;
        };

        // initialize
        this.each(function() {

            // is a textarea?
            if (this.nodeName.toLowerCase() != "textarea") return;

            // set height restrictions
            var p = this.className.match(/expand(\d+)\-*(\d+)*/i);
            this.expandMin = minHeight || (p ? parseInt('0'+p[1], 10) : 0);
            this.expandMax = maxHeight || (p ? parseInt('0'+p[2], 10) : 99999);

            // initial resize
            ResizeTextarea(this);

            // zero vertical padding and add events
            if (!this.Initialized) {
                this.Initialized = true;
                $(this).css("padding-top", 0).css("padding-bottom", 0);
                $(this).bind("keyup", ResizeTextarea).bind("focus", ResizeTextarea);
            }
        });

        return this;
    };

})(jQuery);

As long as I can't answer the question, how about a code review? 只要我不能回答问题,代码审查怎么样?

  • The plugin doesn't apply if you assign just the expand class, despite the documentation. 尽管有文档说明,但如果仅分配expand类,则该插件不适用。
  • It is custom in JavaScript to have functions, method and property names start with a small letter. 在JavaScript中,自定义功能,方法和属性名称以小写字母开头。
  • Browser sniffing is virtually always wrong. 浏览器嗅探实际上总是错误的。 What problem are you trying to solve with that, and does it really apply to all versions of IE (even IE9) and Opera? 您正在尝试解决什么问题,它真的适用于IE(甚至IE9)和Opera的所有版本吗? And even in Standards Mode? 甚至在标准模式下?
  • It is considered bad form to create custom properties on DOM element references, because they are not native JavaScript objects, and thus they might not support custom properties. 在DOM元素引用上创建自定义属性被认为是不好的形式,因为它们不是本机JavaScript对象,因此它们可能不支持自定义属性。 Also there is the danger, that other plugins use the same property names. 还有危险,其他插件使用相同的属性名称。 Instead use jQuery's .data() method and choose more unique property names, for example with a prefix. 而是使用jQuery的.data()方法并选择更多唯一的属性名称,例如带有前缀。
  • I'm not a big fan of using the same parameter for two different things. 我不喜欢在两个不同的事物上使用相同的参数。 At least give the parameter a better name than e , for example eventOrTarget . 至少给参数指定一个比e更好的名称,例如eventOrTarget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM