jQuery: Get height of hidden element in jQuery

后端 未结 14 2242
南方客
南方客 2020-11-22 09:49

I need to get height of an element that is within a div that is hidden. Right now I show the div, get the height, and hide the parent div. This seems a bit silly. Is there a

14条回答
  •  失恋的感觉
    2020-11-22 10:14

    Here's a script I wrote to handle all of jQuery's dimension methods for hidden elements, even descendants of hidden parents. Note that, of course, there's a performance hit using this.

    // Correctly calculate dimensions of hidden elements
    (function($) {
        var originals = {},
            keys = [
                'width',
                'height',
                'innerWidth',
                'innerHeight',
                'outerWidth',
                'outerHeight',
                'offset',
                'scrollTop',
                'scrollLeft'
            ],
            isVisible = function(el) {
                el = $(el);
                el.data('hidden', []);
    
                var visible = true,
                    parents = el.parents(),
                    hiddenData = el.data('hidden');
    
                if(!el.is(':visible')) {
                    visible = false;
                    hiddenData[hiddenData.length] = el;
                }
    
                parents.each(function(i, parent) {
                    parent = $(parent);
                    if(!parent.is(':visible')) {
                        visible = false;
                        hiddenData[hiddenData.length] = parent;
                    }
                });
                return visible;
            };
    
        $.each(keys, function(i, dimension) {
            originals[dimension] = $.fn[dimension];
    
            $.fn[dimension] = function(size) {
                var el = $(this[0]);
    
                if(
                    (
                        size !== undefined &&
                        !(
                            (dimension == 'outerHeight' || 
                                dimension == 'outerWidth') &&
                            (size === true || size === false)
                        )
                    ) ||
                    isVisible(el)
                ) {
                    return originals[dimension].call(this, size);
                }
    
                var hiddenData = el.data('hidden'),
                    topHidden = hiddenData[hiddenData.length - 1],
                    topHiddenClone = topHidden.clone(true),
                    topHiddenDescendants = topHidden.find('*').andSelf(),
                    topHiddenCloneDescendants = topHiddenClone.find('*').andSelf(),
                    elIndex = topHiddenDescendants.index(el[0]),
                    clone = topHiddenCloneDescendants[elIndex],
                    ret;
    
                $.each(hiddenData, function(i, hidden) {
                    var index = topHiddenDescendants.index(hidden);
                    $(topHiddenCloneDescendants[index]).show();
                });
                topHidden.before(topHiddenClone);
    
                if(dimension == 'outerHeight' || dimension == 'outerWidth') {
                    ret = $(clone)[dimension](size ? true : false);
                } else {
                    ret = $(clone)[dimension]();
                }
    
                topHiddenClone.remove();
                return ret;
            };
        });
    })(jQuery);
    

提交回复
热议问题