jQuery: Get height of hidden element in jQuery

后端 未结 14 2236
南方客
南方客 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:18

    You could do something like this, a bit hacky though, forget position if it's already absolute:

    var previousCss  = $("#myDiv").attr("style");
    
    $("#myDiv").css({
        position:   'absolute', // Optional if #myDiv is already absolute
        visibility: 'hidden',
        display:    'block'
    });
    
    optionHeight = $("#myDiv").height();
    
    $("#myDiv").attr("style", previousCss ? previousCss : "");
    
    0 讨论(0)
  • 2020-11-22 10:18

    I've actually resorted to a bit of trickery to deal with this at times. I developed a jQuery scrollbar widget where I encountered the problem that I don't know ahead of time if the scrollable content is a part of a hidden piece of markup or not. Here's what I did:

    // try to grab the height of the elem
    if (this.element.height() > 0) {
        var scroller_height = this.element.height();
        var scroller_width = this.element.width();
    
    // if height is zero, then we're dealing with a hidden element
    } else {
        var copied_elem = this.element.clone()
                          .attr("id", false)
                          .css({visibility:"hidden", display:"block", 
                                   position:"absolute"});
        $("body").append(copied_elem);
        var scroller_height = copied_elem.height();
        var scroller_width = copied_elem.width();
        copied_elem.remove();
    }
    

    This works for the most part, but there's an obvious problem that can potentially come up. If the content you are cloning is styled with CSS that includes references to parent markup in their rules, the cloned content will not contain the appropriate styling, and will likely have slightly different measurements. To get around this, you can make sure that the markup you are cloning has CSS rules applied to it that do not include references to parent markup.

    Also, this didn't come up for me with my scroller widget, but to get the appropriate height of the cloned element, you'll need to set the width to the same width of the parent element. In my case, a CSS width was always applied to the actual element, so I didn't have to worry about this, however, if the element doesn't have a width applied to it, you may need to do some kind of recursive traversal of the element's DOM ancestry to find the appropriate parent element's width.

    0 讨论(0)
  • 2020-11-22 10:18

    Building further on user Nick's answer and user hitautodestruct's plugin on JSBin, I've created a similar jQuery plugin which retrieves both width and height and returns an object containing these values.

    It can be found here: http://jsbin.com/ikogez/3/

    Update

    I've completely redesigned this tiny little plugin as it turned out that the previous version (mentioned above) wasn't really usable in real life environments where a lot of DOM manipulation was happening.

    This is working perfectly:

    /**
     * getSize plugin
     * This plugin can be used to get the width and height from hidden elements in the DOM.
     * It can be used on a jQuery element and will retun an object containing the width
     * and height of that element.
     *
     * Discussed at StackOverflow:
     * http://stackoverflow.com/a/8839261/1146033
     *
     * @author Robin van Baalen <robin@neverwoods.com>
     * @version 1.1
     * 
     * CHANGELOG
     *  1.0 - Initial release
     *  1.1 - Completely revamped internal logic to be compatible with javascript-intense environments
     *
     * @return {object} The returned object is a native javascript object
     *                  (not jQuery, and therefore not chainable!!) that
     *                  contains the width and height of the given element.
     */
    $.fn.getSize = function() {    
        var $wrap = $("<div />").appendTo($("body"));
        $wrap.css({
            "position":   "absolute !important",
            "visibility": "hidden !important",
            "display":    "block !important"
        });
    
        $clone = $(this).clone().appendTo($wrap);
    
        sizes = {
            "width": $clone.width(),
            "height": $clone.height()
        };
    
        $wrap.remove();
    
        return sizes;
    };
    
    0 讨论(0)
  • 2020-11-22 10:19

    By definition, an element only has height if it's visible.

    Just curious: why do you need the height of a hidden element?

    One alternative is to effectively hide an element by putting it behind (using z-index) an overlay of some kind).

    0 讨论(0)
  • 2020-11-22 10:25

    You could also position the hidden div off the screen with a negative margin rather than using display:none, much like a the text indent image replacement technique.

    eg.

    position:absolute;
    left:  -2000px;
    top: 0;
    

    This way the height() is still available.

    0 讨论(0)
  • 2020-11-22 10:26

    I ran into the same problem with getting hidden element width, so I wrote this plugin call jQuery Actual to fix it. Instead of using

    $('#some-element').height();
    

    use

    $('#some-element').actual('height');
    

    will give you the right value for hidden element or element has a hidden parent.

    Full documentation please see here. There is also a demo include in the page.

    Hope this help :)

    0 讨论(0)
提交回复
热议问题