问题描述
我想使用 jquery 检查窗口大小,并根据不同的分辨率更改背景图像.所以我想以某种方式在更多情况下使用switch"语句,但我只是不知道这会是什么样子.这是我想要的基本结构,但有更多选择:
i would like to check for the window size with jquery and based on the different resolutions i would like to change the background image. So i was thinking to somehow use the "switch" statement for more cases, but i just don't know how this would look like. This is the basic structure i want but with more options:
if ((screen.width>=1024) && (screen.height>=768)) {
//do something
}
else {
//do something else
}
感谢您的帮助.
推荐答案
switch
语句不会让你做诸如检查特定值之间的数字之类的事情,它也不会让你检查在多个变量上,要么...
The switch
statement won't let you do stuff like checking for numbers between certain values, and it won't let you check on multiple variables, either...
所以对于这个特定的场景,我认为最合适的实际上只是一个 if-elseif
语句的列表,就像你已经在做的那样.
So for this particular scenario, I think the best fit is actually just a list of if-elseif
statements, like you're already on your way to do.
在 switch
中进行范围检查"真的很冗长:
To do "range checks" in switch
is really verbose:
switch(windowWidth) {
case 1:
case 2:
case 3:
case 4:
case 5:
//Do something if value is less than or equal to 5
break;
case 6:
case 7:
case 8:
case 9:
case 10:
//Do something if value is higher than 5 AND less than or equal to 10
break;
...
...
}
这篇关于如何检测窗口大小然后用 jquery switch 语句做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!