Dynamically resize div based on window size

↘锁芯ラ 提交于 2020-01-02 04:57:05

问题


I'm trying to optimise my website for different resolutions. In the center of the website I have a DIV that currently has a fixed size. I'm trying to make its size (and contents) change according to the size of the browser window. How do I do that? This is my website if you want to take a look at its code: http://www.briefeditions.com


回答1:


If you resize the page the div will resize with it and on load of the page.

$(window).on('load resize', function(){
    $('#div').width($(this).width());
});



回答2:


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
$(document).ready(function(){
       var windowHeight = $(window).height();
       $('#divID').css('min-height',windowHeight+'px');
});

UPDATE

If you want that site will resize based on browser resize then use % instead of px

CSS:

html {height:100%; overflow:hidden}
body {height: 100%;}



回答3:


I guess you need screen width and height for client(users) machine.

at onload of page get screen width & height and set those values to divs using jquery/javascript

var userscreen_width,userscreen_height;
userscreen_width = screen.width;
userscreen_height = screen.height;

check this for more info




回答4:


Keep in mind that in your example iframe also has fixed size. You should also resize it to the parents width. In your example this would work:

$(window).on('load resize', function(){
    $('#content, #content > iframe').width($(this).width());
});

Keep in mind that you must remove all margins, as well as absolute positioning like: top, left, position:absolute from you element styles.




回答5:


I checked the code from the provided link in question.

Change width to 80% in #content style. And in .wrapper change width to 100%.

You have used mainly 920px for width, so whenever you will resize window the control will not re-size. Use equivalent % instead of 920px;




回答6:


You can do like this

var width = $(window).width(); 
$("#divId").width(width);


来源:https://stackoverflow.com/questions/11464679/dynamically-resize-div-based-on-window-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!