浏览器调整比较语句大小
Browser Resize Comparative Statement
我目前的一个jQuery项目需要浏览器调整大小(宽度和高度)功能,一些支持的分辨率相互比较起来很时髦。欢迎提出任何改进这一比较声明的建议。我试着堵住所有的洞,但我感觉还有几个洞。请注意,我也在检查变量"isIos"。
这是脚本:
function getBrowserWidth() {
$(window).load(function () {
if (window.innerWidth) {
return window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth != 0) {
return document.documentElement.clientWidth;
}
else if (document.body) { return document.body.clientWidth; }
return 0;
});
$(window).resize(function () {
if (window.innerWidth) {
return window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth != 0) {
return document.documentElement.clientWidth;
}
else if (document.body) { return document.body.clientWidth; }
return 0;
});
}
function getBrowserHeight() {
$(window).load(function () {
if (window.innerHeight) {
return window.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight != 0) {
return document.documentElement.clientHeight;
}
else if (document.body) { return document.body.clientHeight; }
return 0;
});
$(window).resize(function () {
if (window.innerHeight) {
return window.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight != 0) {
return document.documentElement.clientHeight;
}
else if (document.body) { return document.body.clientHeight; }
return 0;
});
}
var browserWidth = getBrowserWidth(),
browserHeight = getBrowserHeight(),
isIphone = navigator.userAgent.match(/iPhone/i) != null,
isIpod = navigator.userAgent.match(/iPod/i) != null,
isIpad = navigator.userAgent.match(/iPad/i) != null,
isIos = isIphone || isIpod || isIpad;
if (browserWidth <= 1024 && isIos) {
} else if (browserWidth > 800 && browserWidth <= 1024 && !isIos) {
} else if (browserWidth <= 1024 && browserHeight <= 768 && !isIos) {
} else if (browserWidth > 1024 && browserWidth <= 1280) {
} else if (browserWidth > 1024 && browserWidth <= 1280 && browserHeight <= 720) {
} else if (browserWidth > 1280 && browserWidth <= 1600) {
} else if (browserWidth > 1280 && browserWidth <= 1600 && browserHeight > 768 && browserHeight <= 900) {
} else if (browserWidth > 1920 && browserWidth <= 4000) {
} else if (browserWidth > 1920 && browserWidth <= 4000 && browserHeight > 1080 && browserHeight <= 4000) {
} else {
}
如果你真的想做这个方法,那么我建议从最高到最低进行级联比较(有点像switch语句)。这样,你就不需要范围:
var w = $.Window.width();
if(w>4000){
//greater than 4000
} else if(w>1920){
//assumed less than 4000 greater than 1920
} else if (w>1280){
//assumed less than 1920 but greater than 1280
} else if (w>1024){
//assumed less than 1280 but greater than 1024
} else if (w>800){
//assumed less than 1024 but greater than 800
} else {
//small sized
}
相关文章:
- 我如何才能继续'如果'语句来比较作为参数的多个函数返回值
- Switch语句比较用户输入的Javascript
- switch语句中的数字比较失败
- 全局变量和if语句与此的比较
- JsRender:使用 if 语句将 JSON 对象中的值与外部变量进行比较
- 如果语句比较位置
- 在 if 语句之外使用比较参数
- 将变量日期与 if/else 语句中的日期(日期字符串)进行比较
- if/else 语句比较数字
- 如何在“if”语句中比较<选项>“数据名称”
- 无法在 js 中的条件语句中比较 0
- 交换机/比较语句问题
- 若语句不起作用,我将其与变量进行比较
- 比较 IF 语句中的字符串
- 浏览器调整比较语句大小
- 为什么JavaScript在比较一个变量和两个true语句时返回false
- I'm在if语句中使用小于比较器;然而,即使它不应该;t、 它的评估结果为false
- 如果comparison(if语句)比较未定义的值,则返回true
- If语句比较lastName是否以字母A-L开头
- Javascript: if语句比较字符串值不起作用