获取用户设备屏幕尺寸的最小边 JS/jQuery

Get the smallest side of a user device screen size JS/jQuery

我想在页面加载时获取用户设备 display 大小 screen.widthscreen.height(例如:iPhone 7、375x667px ), 然后我需要比较这两个尺寸并使用最小尺寸 (375px) 将其应用于具有 CSS 功能的元素。

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

我是 JavaScript 的新手,所以不知道如何进行第二部分、比较和进一步操作。

如何做到?

您可以使用 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>

正如很多人在评论中所说,我们无法理解为什么不使用 if

您可以使用条件运算符:?:,但它本质上是 if.

您还可以使用带有 min()max() 函数的 Math 库来获取某些值的最小值或最大值(也可用于数组,但这里不是这种情况)。
用法:Math.min(value1, value2)

示例:

var ww = 100;
var wv = 120;
var smallest = Math.min(ww,wv);
console.log(smallest)

进一步阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min

最后,Math.min()max()在内码中也使用if...