【发布时间】:2023-03-27 09:24:01
【问题描述】:
如果用户屏幕小于 1024 像素,我想隐藏一个浮动 div,因为它会覆盖我的博客内容区域。我在网上找到了这个 jQuery,但我不知道如何使用它。
$(document).ready(function() {
if ((screen.width>1024)) {
// if screen size is 1025px wide or larger
$(".yourClass").css('display', 'none'); // you can also use $(".yourClass").hide();
}
elseif ((screen.width<=1024)) {
// if screen size width is less than 1024px
$(".yourClass").css('display', 'block'); // here you can also use show();
}
});
如果我的浮动 div 类名是 sharecontent,我应该像下面这样替换上面的脚本吗?如果是,则它不起作用。
$(document).ready(function() {
if ((screen.width>1024)) {
// if screen size is 1025px wide or larger
$(".sharecontent").css('display', 'none'); // you can also use $(".yourClass").hide();
}
elseif ((screen.width<=1024)) {
// if screen size width is less than 1024px
$(".sharecontent").css('display', 'block'); // here you can also use show();
}
});
我也尝试用 window.width 替换 screen.width 但仍然没有成功:(
【问题讨论】:
-
不要使用 Javascript 来解决非 Javascript 问题。更好地修复 CSS
-
不管怎样,我的屏幕尺寸为什么重要?我的浏览器窗口可能没有全屏显示
-
Gareth 提出了一个重要的观点,你应该看看窗口的宽度,而不是真正的屏幕。
标签: jquery hide resolution show