[英]how to resize a div with the browser window using javascript or jquery
是否可以使用JavaScript或jQuery在浏览器窗口中调整div
高度的大小,以获取Windows对象尺寸并创建函数?
You can bind to the resize
event to well... re-size your element: 您可以绑定到
resize
事件以很好地resize
元素的大小:
//bind event handler to the `resize` event
$(window).on('resize', function () {
//cache the window object for use in this function
var $this = $(this);
//set the height of my object to the height of the window
$('#my-object').height($this.height());
//trigger a window resize to initialize the object's height
}).trigger('resize');
$this
refers to the window
object. $this
引用window
对象。
Note that .on()
is new in jQuery 1.7 and replaces the depreciated .bind()
in this instance. 请注意,
.on()
是jQuery 1.7中的新增功能,并在此实例中替换了已贬值的.bind()
。
Here is a crossbrowser solution: 这是一个跨浏览器解决方案:
I divided the result by 'two' to make the effect more visible at window resize: 我将结果除以“二”,以使在调整窗口大小时效果更加明显:
$.getDocHeight = function(){
var D = document;
return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};
$('#box').height($.getDocHeight()/2 );
$(window).resize(function(){
$('#box').height($.getDocHeight()/2 );
});
You can just use: 您可以使用:
$('#box').height($.getDocHeight);
Hope this helps - Happy coding! 希望对您有所帮助-编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.