1

Is it possible to re-size a div's height with the browser window using JavaScript or jQuery getting the windows object dimensions and creating a function?

2 Answers 2

4

You can bind to the resize event to well... re-size your element:

//bind event handler to the `resize` event
$(window).on('resize', function () {

    //cache the window object for use in this function
    var $this = $(this);

    //set the height of my object to the height of the window
    $('#my-object').height($this.height());

//trigger a window resize to initialize the object's height
}).trigger('resize');

$this refers to the window object.

Note that .on() is new in jQuery 1.7 and replaces the depreciated .bind() in this instance.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a crossbrowser solution:

DEMO

I divided the result by 'two' to make the effect more visible at window resize:

$.getDocHeight = function(){
  var D = document;
  return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};

$('#box').height($.getDocHeight()/2 );

$(window).resize(function(){
  $('#box').height($.getDocHeight()/2 );
});

You can just use:

$('#box').height($.getDocHeight);

Hope this helps - Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.