简体   繁体   English

jQuery获取窗口大小并应用于div CSS并根据浏览器调整大小

[英]Jquery get size of window and apply to div css and adjust on browser resize

Ok so I want to make a div full screen. 好的,所以我要使div全屏显示。 The only way I can do this is through JavaSript/jQuery. 我能做到这一点的唯一方法是通过JavaSript / jQuery。 CSS won't work with this one. CSS不能与此一起使用。

I want to get the size of the browser and apply the size to a css of a div. 我想获取浏览器的大小并将该大小应用于div的CSS。

$(function(){

  var wheight = $(window).height();

  $(window).resize(function() {
    wheight = $(window).height();
  });

  $('.slide').css({"height":wheight+"px"});

});

It kind of works however the window resize section wont work and the css does not adjust on the fly to the browsers height. 这样做是可行的,但是窗口调整大小部分将无法工作,并且CSS不会根据浏览器的高度即时调整。

Any idea where I have gone wrong? 知道我哪里出错了吗?

Thanks 谢谢

You are only setting the size of the element once and not everytime the window is resized. 您只设置一次元素的大小,而不是每次调整窗口大小时。 This is how I would've done it: 这就是我要做的:

$(function () {

    //Adjust element dimensions on resize event
    $(window).on('resize', function () {
        $('.slide').css('height', $(this).height() + "px");
    });

    //Trigger the event
    $(window).trigger('resize');
});

Try this: 尝试这个:

$(function(){

   var wheight = $(window).height();

   $(window).resize(function() {
      wheight = $(window).height();
      $('.slide').css({"height":wheight+"px"});
    });

});

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

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