【问题标题】:CSS retina and non retina media queriesCSS 视网膜和非视网膜媒体查询
【发布时间】:2015-08-21 00:13:44
【问题描述】:

我正在尝试根据不同的分辨率为我的网站设置背景图片。我还想包括视网膜媒体查询,这样如果视网膜设备访问我的网站,就会加载正确的图像。问题是我总是得到最高分辨率,当然不是视网膜......这是我的 css

 <style>


#background
{
    font-size: large;
    position: relative;
    top:50%;
    left:50%;
    color: white;

}

  @media only screen and (min-device-width : 768px) and (max-device-width:1024px) and (orientation : landscape)and (-webkit-min-device-pixel-ratio: 2) 
{
    #demo{
    width: 100%;

    min-height: 800px;
    background-size: 100%;
    background-repeat: no-repeat;
    position:absolute;
    background-image: url(demo_BG_1024@2x.png);
    background-color: rgb(0,0,0);
    }
}

@media screen and (min-width:1025px)
{
    #demo{
    width: 100%;

    min-height: 800px;
    background-size: 100%;
    background-repeat: no-repeat;
    position:absolute;
    background-image: url(demo_BG_1024.png);
    background-color: rgb(0,0,0);
    }
}

@media screen and (max-width:1024px)
{
    #demo{
    width: 100%;

    min-height: 800px;
    background-size: 100%;
    background-repeat: no-repeat;
    position:absolute;
    background-image: url(demo_BG_1024.png);
    background-color: rgb(0,0,0);
    }
}

@media screen and (max-width:720px)
{
    #demo{
    width: 100%;

    min-height: 800px;
    background-size: 100%;
    background-repeat: no-repeat;
    position:absolute;
    background-image: url(demo_BG_720.png);
    background-color: rgb(0,0,0);
    }
}

@media screen and (max-width:320px)
{
        #demo{
        width: 100%;

        height: 100%;
        background-size: 100%;
        background-repeat: no-repeat;
        position:absolute;
        background-image: url(demo_BG_320.png);
        background-color: rgb(0,0,0);
    } 
}
</style>

【问题讨论】:

    标签: html css retina-display


    【解决方案1】:

    非视网膜用户必须使用-webkit-min-device-pixel-ratio:1

    非视网膜。

     @media only screen and (min-device-width : 768px) and (max-device-width:1024px) and (orientation : landscape)and (-webkit-min-device-pixel-ratio: 1) 
    {
        #demo{
        width: 100%;
        min-height: 800px;
        background-size: 100%;
        background-repeat: no-repeat;
        position:absolute;
        background-image: url(demo_BG_1024@1x.png);
        background-color: rgb(0,0,0);
        }
    }
    

    视网膜

     @media only screen and (min-device-width : 768px) and (max-device-width:1024px) and (orientation : landscape)and (-webkit-min-device-pixel-ratio: 2) 
    {
        #demo{
        width: 100%;
        min-height: 800px;
        background-size: 100%;
        background-repeat: no-repeat;
        position:absolute;
        background-image: url(demo_BG_1024@2x.png);
        background-color: rgb(0,0,0);
        }
    }
    

    【讨论】:

    • 感谢您的建议 ;) 为什么 firefox 现在不显示背景?
    • 只有火狐?它不应该在任何浏览器上工作,因为我使用 1x.png 而不是你的 2x.png 图像用于非视网膜媒体。
    • 您能否提供一个工作示例,因为我现在完全迷路了?我是视网膜网站的新手。谢谢
    【解决方案2】:

    要补充@BaTmaN 的答案,它很可能在 Firefox bc 中不起作用/示例代码使用 webkit 前缀。 对于其他浏览器,您需要使用正确的浏览器前缀: 例如。

    only screen and (-moz-min-device-pixel-ratio: 1.5),
    only screen and (-o-min-device-pixel-ratio: 3/2),
    only screen and (min-device-pixel-ratio: 1.5)
    

    【讨论】:

    • min-device-pixel-ratio 不是标准。任何浏览器都不支持无前缀版本。唯一有用的符号是带有 webkit 前缀的符号,其他浏览器添加 min-resolution
    【解决方案3】:

    我不使用媒体查询,我使用的是 Retina.js。您需要做的就是包含retina.js 文件并托管两个版本的图像-image.pngimage@2x.png(例如)。

    然后,JavaScript 将检查页面上的每个图像,如果可用,则在视网膜显示器上自动显示高分辨率图像(@2x - Apple 的预定义标识符),如果不可用,则显示正常图像。非视网膜显示器将提供非 @2x 版本。

    更多信息在这里:http://imulus.github.io/retinajs/

    【讨论】:

    • 对不起,我投了反对票,因为 op 明确要求媒体查询并用 html、css 和视网膜显示标记。你怎么能用 Javascript 解决方案来回答这个问题?我不明白。
    • @inta 我提供了一个操作员可能没有考虑过的替代选项,这是一种更简单的方法。例如,我在尝试使用媒体查询时遇到了问题,因为我没有听说过retina.js,但实际上它是一种更简单的方法。
    猜你喜欢
    • 2015-10-13
    • 2023-03-08
    • 1970-01-01
    • 2013-02-10
    • 2014-03-19
    • 2013-09-28
    • 1970-01-01
    • 2012-10-24
    • 2013-09-27
    相关资源
    最近更新 更多