jQuery 根据用户屏幕大小设置div宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6762564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:21:30  来源:igfitidea点击:

setting div width according to the screen size of user

jquerywidthscreen

提问by Prashant Singh

I am trying to set the width and height of a div exactly equal to the size of my users screen. How can it be done. Should I use jquery ? If so, how ?

我试图将 div 的宽度和高度设置为完全等于我的用户屏幕的大小。怎么做到呢。我应该使用 jquery 吗?如果是这样,如何?

回答by pimvdb

What about some CSS? http://jsfiddle.net/TJGZU/

一些CSS怎么样?http://jsfiddle.net/TJGZU/

body, html { /* set size of body to full page and remove margins */
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}

div { /* set div to full width and height */
    width: 100%;
    height: 100%;
}

回答by abhijit

by css seems ideal as pimvdb has suggested...doing it in jquery would be inline styling which would be a bad practice which can be avoided unless necessary.

by css 似乎很理想,正如 pimvdb 所建议的那样......在 jquery 中这样做将是内联样式,这将是一种不好的做法,除非必要,否则可以避免。

by means of jquery:

通过jquery:

    var windowWidth = $(window).width();
    var windowHeight =$(window).height();
    $('div').css({'width':windowWidth ,'height':windowHeight });

Edited Part:

编辑部分:

var windowWidth = ((parseInt($(window).width())) / 2) - 120;

like this whatever height and width seems ideal for you, you can obtain via this.

像这样无论高度和宽度对你来说都是理想的,你都可以通过这个获得。

回答by user2958974

below is a sample code for your problem:

以下是您的问题的示例代码:

var windowWidth = ((parseInt($(window).width())) / 2) - 120;

Also, note that this answer makes use of jQuery.

另请注意,此答案使用了 jQuery。