简体   繁体   English

jQuery屏幕分辨率高度调整

[英]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. 为了更好地平衡我正在处理的页面,我想找到一种方法来根据屏幕分辨率增加DIV的上边距。 What is my best way to set these dimensions with jQuery or Javascript? 使用jQuery或Javascript设置这些维度的最佳方法是什么?

To get screen resolution in JS use screen object 要在JS中使用screen对象获取屏幕分辨率

screen.height;
screen.width;

Based on that values you can calculate your margin to whatever suits you. 根据这些值,您可以计算适合您的保证金。

Here is an example on how to center an object vertically with jQuery: 这是一个如何使用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. 但您可以轻松地将其更改为您的需求。

Another example for vertically and horizontally centered div or any object(s): 垂直和水平居中的div或任何对象的另一个例子:

 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 });

查看jQuery 维度插件

var space = $(window).height();
var diff = space - HEIGHT;
var margin = (diff > 0) ? (space - HEIGHT)/2 : 0;
$('#container').css({'margin-top': margin});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM