如何检测窗口大小,然后使用 jquery switch 语句执行某些操作
我想用 jquery 检查窗口大小,并根据不同的分辨率我想更改背景图像。所以我想以某种方式在更多情况下使用“switch”语句,但我只是不知道这会是什么样子。这是我想要的基本结构,但有更多选项:
if ((screen.width>=1024) && (screen.height>=768)) {
//do something
}
else {
//do something else
}
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用:
然后您可以执行以下操作:
编辑 - 我认为在这种情况下使用 switch 语句没有用。 switch 语句只是 if...else 表示法的另一种方式,在这种情况下我发现更有用,因为您需要进行多次比较:
You should use:
and then you could do:
EDIT - i don't think that using a switch statement is useful in this case. The switch statement is just an alternate way for the if...else notation that in this case i find more useful because you have multiple comparison to do:
switch
语句不会让你做一些事情,比如检查某些值之间的数字,它也不会让你检查多个变量......所以对于这个特定的场景,我认为best fit 实际上只是一个
if-elseif
语句列表,就像您已经在做的事情一样。在
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...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: