jQuery:根据浏览器窗口分辨率动态添加类
你好朋友,我正在尝试根据浏览器窗口分辨率动态向主体添加一个类。这是我尝试使用的代码,但需要帮助调整它,因为我根本不了解 jQuery。
我想要实现的选项是:
一旦访问者来到我的网站,此代码必须检查他的浏览器窗口大小并按照以下规则将类添加到正文
如果窗口大小大于 1024px 但小于 1280px,则添加类
.w1280
。如果窗口大小大于1280px但小于1440px,则添加类
.w1440
。如果窗口大小大于1440px但小于1280px,则添加类
.w1680
。如果窗口大小大于1680px,则添加类
.wLarge
。
为了实现这一目标,我尝试使用以下脚本。我的问题是:
这是正确的代码吗?如果不是,正确的代码是什么?
这是最好的最短代码吗?如果不是,正确的代码是什么?
请帮助我,因为我对 jQuery 的了解几乎为零。
function checkWindowSize() {
if ( $(window).width() > 1024) {
$('body').addClass('w1280');
} else {
$('body').removeClass('w1280');
}
if ( $(window).width() > 1280 ) {
$('body').addClass('w1440');
} else {
$('body').removeClass('w1440');
}
if ( $(window).width() > 1440) {
$('body').addClass('w1680');
} else {
$('body').removeClass('w1680');
}
if ( $(window).width() > 1600) {
$('body').addClass('wLarge');
} else {
$('body').removeClass('wLarge');
}
}
checkWindowSize()
Hello friends I am trying to add a class to body dynamically depending on the browser window resolution. Here is the code I am trying to use but need help tuning it as I dont know jQuery at all.
The options I want to achieve are :
Once a visitor comes to my site, this code must check his browser windows size and add class to body as per the following rules
If window size is more than 1024px but less than 1280px then add class
.w1280
.If window size is more than 1280px but less than 1440px then add class
.w1440
.If window size is more than 1440px but less than 1280px then add class
.w1680
.If window size is more than 1680px then add class
.wLarge
.
To achieve this, I am trying to use the following script. My questions are:
Is this the correct code? If not what is the correct code?
Is this the best shortest possible code? If not what will be the correct code?
Kindly help as my knowledge of jQuery is almost ZERO.
function checkWindowSize() {
if ( $(window).width() > 1024) {
$('body').addClass('w1280');
} else {
$('body').removeClass('w1280');
}
if ( $(window).width() > 1280 ) {
$('body').addClass('w1440');
} else {
$('body').removeClass('w1440');
}
if ( $(window).width() > 1440) {
$('body').addClass('w1680');
} else {
$('body').removeClass('w1680');
}
if ( $(window).width() > 1600) {
$('body').addClass('wLarge');
} else {
$('body').removeClass('wLarge');
}
}
checkWindowSize()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您没有在
body
元素上存储任何其他类,您可以这样做:有些人可能会建议您使用
switch
语句来执行此操作,但是,有些人可能会建议您使用switch
语句来执行此操作 。人们也喜欢吃自己的孩子。这个函数每次调用时都会覆盖
body
的类(默认情况下,如果浏览器小于/等于 1024 像素,则根本没有类),所以就像我说的那样,它不会如果您的body
有其他需要维护的类,则不起作用。编辑根据 Šime 的建议,这是一种更安全的方法:
If you aren't storing any other classes on the
body
element, you could do this:Some people might advise you to do it with a
switch
statement, but then, some people also like to eat their young.This function will overwrite the
body
's class every time it's called (the default, if the browser is smaller than/equal to 1024 pixels, is no class at all), so like I said it won't work if yourbody
has other classes that need to be maintained.EDIT Per Šime's suggestions, here's a safer way to do it:
这对我有用,感谢 Naina 的评论:
https://www.quora.com/How-do-I-add-and-remove-CSS-classes-using-jQuery-based-on-the-screen-size
This worked for me thanks to Naina's comment here:
https://www.quora.com/How-do-I-add-and-remove-CSS-classes-using-jQuery-based-on-the-screen-size