简体   繁体   English

不同图像尺寸的台式机和手机

[英]Different Image Size Desktop & Mobile

I just did a speed test on desktop & mobile on gtmetrix and have the error code: Serve Scaled Images - But only on the mobile speed test. 我只是在gtmetrix上的台式机和移动设备上进行了速度测试,并显示错误代码:Serve Scaled Images-但是仅在移动速度测试上。 The error reads as follows: 错误内容如下:

* * .jpg is resized in HTML or CSS from 1200x431 to 318x114. * * .jpg在HTML或CSS中的大小从1200x431调整为318x114。 Serving a scaled image could save 520.9KiB (92% reduction). 提供缩放后的图像可以节省520.9KiB(减少92%)。

Is there a specific code I can put with the image to have it one size when on mobile and leave the desktop one at the original/other size. 我可以在图像上放一个特定的代码,以使其在移动设备上具有一种尺寸,而在桌面上保持原始/其他尺寸。 Or is there another way such as serve a particular image for mobile (same image smaller size) then another image for serving desktops? 还是有另一种方式,例如为移动设备提供特定的图像(较小尺寸的相同图像),然后再为桌面提供其他图像?

Thanks. 谢谢。

You can do like this and define different background url on different media queries for same class. 您可以这样做,并在同一类的不同媒体查询上定义不同的背景URL。

    /* Smartphones (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
/* Styles */
}

/* iPads (landscape) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
/* Styles */
}

I think you might want something like this: 我认为您可能想要这样的事情:

    var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

if(isMobile.any()){
    // Mobile!
} else {
    // Not mobile
}

You can then do something like: (untested below(needs tweaking)) 然后,您可以执行以下操作:(未试过(需要调整))

if(isMobile.any()){
img { height:140%;width:140%

You can alter the size of images through the style tag. 您可以通过样式标签更改图像的大小。 img { should alter all <img src tags on the page as I've done it before. img {应该像我之前所做的那样更改页面上的所有<img src标签。 And a revision of the above code should alter it only for mobile or desktop computers. 修改以上代码后,应仅对移动或台式计算机进行更改。

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

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