我正在开发一个WordPress主题,尝试将视网膜屏幕支持的CSS查询整合到我的CSS文件中。
在更换所有背景图片之前,我只想澄清一下,我已经正确设置了媒体查询。
- 我已将所有的背景图片大小加倍,并加上了"@2x"命名约定。例如:
icon-user@2x.png
。 - 我在我的代码中添加了一个jQuery函数来替换CSS类为
hires
的图像。 - 在我的CSS文档中,我有一个用于背景图片的普通CSS类。
普通CSS查询:
.side-nav .arrow {
background: url(../images/arrow-nav.png) no-repeat top left;
width: 5px;
height: 8px;
display: inline-block;
margin-left: 10px
}
这是我为支持视网膜设备更改.side-nav .arrow类的正确方式吗?在声明背景大小时,我是否保留原始较小图像的尺寸?
/* All Retina Ready devices larger than 1.5 pixel ratio */
@media only screen and (-moz-min-device-pixel-ratio: 1.5),
only screen and (-o-min-device-pixel-ratio: 3/2),
only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and (min-device-pixel-ratio: 1.5) {
.side-nav .arrow {
background-image:url(../images/arrow-nav@2x.png);
-webkit-background-size:5px 8px;
-moz-background-size:5px 8px;
-o-background-size:5px 8px;
background-size:5px 8px
}
}
jQuery代码
$(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;
}
}
});
谢谢你