如何检测窗口大小,然后用jquery switch语句做一些事情 [英] How to detect window size and then do something with jquery switch statement

查看:158
本文介绍了如何检测窗口大小,然后用jquery switch语句做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查的窗口大小与jquery和基于不同的分辨率我想改变背景图像。所以我想在某种程度上使用switch语句更多的情况,但我只是不知道这将是什么样子。这是我想要的基本结构,但有更多的选项:

  if((screen.width> = 1024)&& (screen.height> = 768)){
// do something
}
else {
//做别的事
}
解决方案

p> switch 语句不会让你做某些值之间的数字检查,它不会让你检查多个变量,... ...



因此,对于这种特殊情况,我认为最佳拟合实际上只是一个 if-elseif 语句



switch 中执行范围检查确实是冗长的:

  switch(windowWidth){
case 1:
case 2:
case 3:
case 4:
case 5:
//如果值小于或等于5,执行操作
break;
case 6:
case 7:
case 8:
case 9:
case 10:
//如果值高于5,小于或等于10
break;
...
...
}


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
}

Thanks for your help.

解决方案

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...

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.

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语句做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆