简体   繁体   English

CSS媒体查询与JavaScript

[英]CSS Media Queries vs. JavaScript

I have CSS3 Media Queries in my stylesheet to adjust for small and large screens. 我的样式表中有CSS3 Media Queries,可针对大屏幕和小屏幕进行调整。 Mobile devices are not a concern for this project. 移动设备不是此项目的关注点。

IE8 is the most used device by visitors to the website, and needs to be supported. IE8是网站访问者使用最多的设备,需要得到支持。 I don't see IE8 supporting CSS3 Media Queries in my tests. 我的测试中没有看到IE8支持CSS3媒体查询。

Is there any unforeseen consequence of converting the media queries to JavaScript? 将媒体查询转换为JavaScript是否有不可预见的后果? Can I keep both? 我可以两个都保留吗?

Media Queries: 媒体查询:

@media only screen 
and (max-width : 800px) {
    body {
        font-size: 8pt;
        line-height: 1.4em;
        }
    #header h1 {
        width: 48% !important;
    }

    .inner {
        width: 95% !important;
    }

    #feature {
        height: 23em !important;
    }

    #feature .overlay {
        font-size: 120% !important;
        line-height: 1.3em !important;
    }

    #feature .overlay h1 {
        font-size: 150% !important;
        line-height: 1.2em !important;
    }
}

@media only screen 
and (max-width : 1024px) {
    body {
        font-size: 9pt;
        line-height: 1.4em;
        }
    #header h1 {
        width: 46% !important;
    }

    .inner {
        width: 95% !important;
    }
}

@media only screen 
and (min-width : 1600px) {
    body {
        font-size: 14pt;
        line-height: 1.4em;
    }

    .inner {
        width: 82% !important;
    }
}

Proposed Change CSS: 拟议变更CSS:

body.small {
    font-size: 8pt;
    line-height: 1.4em;
    }
.small #header h1 {
    width: 48% !important;
}

.small .inner {
    width: 95% !important;
}

.small #feature {
    height: 23em !important;
}

.small #feature .overlay {
    font-size: 120% !important;
    line-height: 1.3em !important;
}

.small #feature .overlay h1 {
    font-size: 150% !important;
    line-height: 1.2em !important;
}


body.medium {
    font-size: 9pt;
    line-height: 1.4em;
    }
.medium #header h1 {
    width: 46% !important;
}

.medium .inner {
    width: 95% !important;
}


body.highdef {
    font-size: 14pt;
    line-height: 1.4em;
}

.highdef .inner {
    width: 82% !important;
}

Proposed Change JavaScript: 建议的更改JavaScript:

$(document).ready( function(){
    check_dimensions();
    $(window).on( "resize", function(){
        check_dimensions();
    });
});

function check_dimensions(){
    var outer_width = $(this).outerWidth();

    $("body").removeClass();
    if ( outer_width >= 1600 ){
        $("body").addCass("highdef");
    } else if ( outer_width <= 800 ){
        $("body").addClass("small");
    } else if ( outer_width > 800 && outer_width <= 1024 ){
        $("body").addClass("medium");
    }
}

I know this question is rooted in theory and perhaps not an appropriate fit for SO, but hopeful some of you may have thoughts before you vote to close ;) 我知道这个问题源于理论,也许不适合SO,但是希望你们中的一些人在投票结束之前可能有想法;)

There is a wonderful script that make media queries to work on older browser. 有一个很棒的脚本,可以使媒体查询在较旧的浏览器上运行。 Here is the link to Respond.js Hope this help! 是Respond.js的链接,希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM