How to get an element the same height as browser resolution?

后端 未结 5 1932
误落风尘
误落风尘 2021-01-27 14:59

I am trying use jquery to set the height of an element to equal the users browser. Here is the code I got so far that\'s not working. Is it because I have a minimal height set

5条回答
  •  忘掉有多难
    2021-01-27 15:34

    Try this, it keeps the image's aspect ratio

    $(function(){
    var ratio = $("img").width() / $("img").height();
    var newWidth = ($(window).height()-10) * ratio;
    if(newWidth > $(window).width())
            $("img").css("width","100%");
        else
            $("img").css("width",newWidth+"px");
    $(window).resize(function(){
        var ratio = $("img").width() / $("img").height();
        var newWidth = $(window).height() * ratio;
        if(newWidth > $(window).width()){
            $("img").css("width","100%");
    
            }
        else{
            $("img").css("width",newWidth+"px");
    
            }
    });
    });
    

提交回复
热议问题