views:

65

answers:

5

You can read your browser dimensions with jQuerys $document.height or with simple JS via height: window.innerHeight || document.body.clientHeight.

But how do I apply the value to a CSS class?

+1  A: 

jQuery:

$('.targetClass').css('height',otherObject.height()+'px');
mVChr
Don't explain too much.
ChrisBenyamin
A: 

You cannot rewrite/add new values to CSS properties/classes on the fly; you can write/rewrite/add values to elements.

You should post your situation, maybe there is a different way to do what you're trying to do.

Jason
Okay, so taking the function mentioned before, I can override the height of my element. Must the function been loaded after the DOM is builded (onDomLoad?)?Situation: Create several DIVs with browsers height. So each DIV has the browsers height and only one DIV is visible at one time (the other DIVs are shown when you scroll down/up).
ChrisBenyamin
A: 
$('#target').css('height',$(document.body).height()+'px')

(jQuery)

Diodeus
A: 

Find the rule object in a browser-specific way ( http://www.javascriptkit.com/domref/cssrule.shtml )

Then you can edit the style object of the rule object and actually change the CSS class definition

seanizer
A: 

No need to overcomplicate this - you don't need to find a way to apply the height to a CSS class. Just give each of your <div>s a class in the HTML:

<div class="viewport_height">This is some content.</div>

then use jQuery code similar to this:

var document_height = $(document).height();
$('.viewport_height').css('height', document_height + 'px');

to apply the measured height to all elements of that class.

For a more robust solution, attach a function to the window's onresize event to recalculate and apply the height whenever the viewport height changes.

Bungle