首页 > 解决方案 > Get the smallest side of a user device screen size JS/jQuery

问题描述

I want to get user device display size with screen.width and screen.height on page load (for example: iPhone 7, 375x667px), then I need to compare these two sizes and use the smallest size (375px) to apply it to an element with CSS function.

function() {
  var ww = screen.width;
  var wh = screen.height;   
}

I'm new to JavaScript so don't know how to do the second part, comparison and further manipulation.

How can it be done?

标签: javascriptjqueryscreen

解决方案


您可以使用 jquery 来执行此操作,

$(document).ready(function(){
        var smallest;
        var winwid = $(window).width();
        var winheight = $(window).height();
        if(winwid < winheight ){
            smallest = winwid;
            alert('Width is smaller than height: '+winwid);
        }
        else {
            smallest = winheight;
            alert('Height is smaller than width: '+winheight);
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


推荐阅读