【问题标题】:How does .@2x work [duplicate].@2x 如何工作[重复]
【发布时间】:2013-09-03 05:07:27
【问题描述】:

我是格式化代码以适应 iphone(尤其是 4 和 5)的新手。您是否向 css 添加了两个图像(没有媒体查询)?或者您是否需要带有该背景图片的媒体查询?

【问题讨论】:

    标签: iphone background background-image


    【解决方案1】:

    基本上是的,您需要执行以下操作:

        .repeatingPattern {
         background: url(../images/bgPattern.png) repeat;
         background-size: 100px 100px;
    }
    
    @media only screen and (-webkit-min-device-pixel-ratio: 2) {
         .repeatingPattern {
              background: url(../images/bgPattern@2x.png) repeat;
         }
    }
    

    但如果你使用 jquery,这样做会更有效率:

    <img class="hires" alt="" src="search.png" width="100" height="100" />
    <script type="text/javascript">
    $(function () {
    
        if (window.devicePixelRatio == 2) {
    
              var images = $("img.hires");
    
              // loop through the images and make them hi-res
              for(var i = 0; i < images.length; i++) {
    
                // create new image name
                var imageType = images[i].src.substr(-4);
                var imageName = images[i].src.substr(0, images[i].src.length - 4);
                imageName += "@2x" + imageType;
    
                //rename image
                images[i].src = imageName;
              }
         }
    
    });
    </script>
    

    【讨论】:

    • 我遇到了很多背景图像无法正确渲染的问题。如果您在 Iphone IOS、移动 safari 方面效率很高 - 我们可以聊聊吗?
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2012-06-25
    • 2014-02-18
    • 2021-05-12
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多