Click here to Skip to main content
15,893,487 members
Articles / Programming Languages / Javascript
Tip/Trick

Dock Height of DOM Elements with jQuery

Rate me:
Please Sign up or sign in to vote.
4.22/5 (4 votes)
12 Nov 2013CPOL 16.9K   2   8
Dock height of DOM elements by extending JQuery objects

Introduction

This tip shows how to dock height of DOM elements by extending jquery objects. It docks any DOM element to its parent height for any size and also fires on resize event.

Using the Code

jQuery dockHeightToParent extend function is as given below:

JavaScript
$.fn.extend({
    dockHeightToParent: function (list, margin) {
        var objTarget = new ObjToDock($(this).selector, list, margin);
        initSizeHeight(objTarget);

        function ObjToDock(selector, lstItemsSubsact, marginHeight) {
            this.selector = selector;
            this.JqueryObjects = $(selector);

            this.listElementsSubstractHeight = lstItemsSubsact;
            this.marginHeight = ((marginHeight!=null) ? marginHeight : 0);
        }

        function initSizeHeight(obj) {
            if (window.attachEvent) {
                window.attachEvent("onresize", setHeight.bind(obj));
            } else {
                window.addEventListener("resize", setHeight.bind(obj), false);
            }

            setHeight(obj);
        }

        var hold = false;
        function setHeight(obj) {
            var objectTarget = ((obj.JqueryObjects != undefined) ? obj : this);

            var selector = objectTarget.selector;
            var marginHeight = objectTarget.marginHeight;

            var parent = $(selector).parent();
            if (parent.is('body')) {
                parent = $(window);
            }
            var parentHeight = parent.outerHeight(true);

            if (obj.JqueryObjects == undefined && hold) return;
            setTimeout(function () { hold = false; }, 100);
            hold = true;

            var totalSubstract = 0;
            if (objectTarget.listElementsSubstractHeight != null) {
                for (var i = 0; i < objectTarget.listElementsSubstractHeight.length; i++) {
                    totalSubstract += $(objectTarget.listElementsSubstractHeight[i]).outerHeight(true);
                }
            }

            totalSubstract += marginHeight;

            $(selector).css("height", parentHeight - totalSubstract + "px");
        }
    }
}); 

Sample of use:

JavaScript
<script>
    //Supose next html code
    //<div class="h2"></div>
    //<div class="toolbar"></div>
    //<div class="grid">
    //    [html Of Grid]
    //</div>

    //Then calling  dockHeightToParent for selector ".grid"
    //with list of elements To Substract Height ["h2", ".divToolbar"]
    //and optional numeric value (30)
    //"grid" element will dock height to It's parent(windows) size and when it's resized
    $(".grid").dockHeightToParent(["h2", ".divToolbar"], 30);
</script>

Image sample:

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
MB Seifollahi10-Nov-13 23:52
professionalMB Seifollahi10-Nov-13 23:52 
could you show an image example (in the content PLZ)
GeneralRe: My vote of 3 Pin
Jaume González11-Nov-13 22:38
Jaume González11-Nov-13 22:38 
GeneralRe: My vote of 3 Pin
Jaume González11-Nov-13 22:52
Jaume González11-Nov-13 22:52 
QuestionCould you post a working example? Pin
Dana Cobb5-Nov-13 5:00
Dana Cobb5-Nov-13 5:00 
AnswerRe: Could you post a working example? Pin
Jaume González5-Nov-13 7:38
Jaume González5-Nov-13 7:38 
GeneralRe: Could you post a working example? Pin
Dana Cobb5-Nov-13 7:40
Dana Cobb5-Nov-13 7:40 
GeneralRe: Could you post a working example? Pin
Jaume González5-Nov-13 8:28
Jaume González5-Nov-13 8:28 
GeneralRe: Could you post a working example? Pin
Jaume González5-Nov-13 8:30
Jaume González5-Nov-13 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.