javascript - 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?
解决方案
您可以使用 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>
推荐阅读
- python - 如何在熊猫中拆分和存储数据框中的数据
- c++ - OPENSSL 将二进制字节转换为 SHA256 C++
- random - 我想出了这两种方法来获得 5 个唯一的随机数。哪个更好/更有效?有没有其他更有效的方法?
- r - 仅从ggplot中的一层删除轮廓颜色
- java - Spring Boot JPA 不会在单个测试范围内保留更改
- python - 如何遍历python中多个字典中的json键、值
- php - 使用 jQuery、Ajax 和 PHP 的动态选择框
- mocking - 模拟时如何跳出循环
- javascript - Chrome 会缓存所有网页吗?
- jenkins - 如何从 Jenkins 作业创建 Github 版本(理想情况下包含自上次构建/发布以来的提交列表)?