How to detect window size and then do something with jquery switch statement(如何检测窗口大小然后用 jquery switch 语句做一些事情)
问题描述
我想使用 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 语句做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测窗口大小然后用 jquery switch 语句做一些事情


- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01