使用窗口大小jQuery动态调整元素大小 [英] Dynamically resize element with window size jquery

查看:219
本文介绍了使用窗口大小jQuery动态调整元素大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了很多与此主题有关的问题,似乎无法找出我的代码有什么问题.任何帮助将不胜感激!

I've looked at many questions regarding this subject and can not seem to find out what is wrong with my code. Any help would be greatly appreciated!

$(window).resize(function(){
   var newwidth = $(window).innerWidth();
   var newheight = $(window).innerHeight();      
   $("#element").height(newheight).width(newwidth);
       });

如果窗口已调整大小,我正在尝试将#element的大小调整为与窗口相同的高度和宽度.

I'm trying to resize #element to the same height and width as the window if the window is resized.

推荐答案

关于.innerWidth(),来自文档:

About .innerWidth(), from docs:

此方法不适用于窗口和文档对象;为了 这些,请改用.width().

This method is not applicable to window and document objects; for these, use .width() instead.

.innerHeight()也有相同的注释.

因此您需要使用.width().height():

$(window).resize(function(){
    var newwidth = $(window).width();
    var newheight = $(window).height();      
    $("#element").height(newheight).width(newwidth);
});

这篇关于使用窗口大小jQuery动态调整元素大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆