jquery - 如果屏幕小于一定宽度则隐藏 div

标签 jquery hide resolution show

如果用户屏幕 < 1024px,我想隐藏 float 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();
}
});

如果我的 float 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 但仍然没有成功:(

最佳答案

使用media queries 。您的 CSS 代码将是:

@media screen and (max-width: 1024px) {
    .yourClass {
        display: none !important;
    }
}

关于jquery - 如果屏幕小于一定宽度则隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4296012/

相关文章:

wpf - WPF应用程序开始最大化,但屏幕分辨率与显示不匹配

javascript - 无法从动态创建的表中删除多余的行

javascript - Ajax .done() 无数据

javascript - 我在 Flask 应用程序中使用 $.(get) 请求时遇到的 JavaScript 问题

javascript - jQuery 隐藏元素和自由空间

jquery - 如何使用jquery显示隐藏两个图像循环4次

ios - 布局约束将图像大小调整为屏幕宽度并保持纵横比

objective-c - Objective C - 如何获得当前的屏幕分辨率?

javascript - 如何从同名的多个输入文本字段中仅获取一个值?

jQuery:为什么这个元素不会在悬停时隐藏?