Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Screen Resolution Height Adjustment

In order to better balance out a page I am working on I would like to find a way to increase the top margin of a DIV depending on the screen resolution. What is my best way to set these dimensions with jQuery or Javascript?

like image 235
Brad Birdsall Avatar asked Aug 13 '09 12:08

Brad Birdsall


4 Answers

To get screen resolution in JS use screen object

screen.height;
screen.width;

Based on that values you can calculate your margin to whatever suits you.

like image 68
RaYell Avatar answered Sep 20 '22 01:09

RaYell


Here is an example on how to center an object vertically with jQuery:

var div= $('#div_SomeDivYouWantToAdjust');
div.css("top", ($(window).height() - div.height())/2  + 'px');

But you could easily change that to whatever your needs are.

like image 45
Bryan Denny Avatar answered Sep 22 '22 01:09

Bryan Denny


Another example for vertically and horizontally centered div or any object(s):

 var obj = $("#divID");
 var halfsc = $(window).height()/2;
 var halfh = $(obj).height() / 2; 

 var halfscrn = screen.width/2;
 var halfobj =$(obj).width() / 2; 

 var goRight =  halfscrn - halfobj ;
 var goBottom = halfsc - halfh;

 $(obj).css({marginLeft: goRight }).css({marginTop: goBottom });
like image 5
umutkeskin Avatar answered Sep 20 '22 01:09

umutkeskin


Check out the jQuery dimensions plugin

like image 4
Colin Avatar answered Sep 20 '22 01:09

Colin