Twitter Bootstrap的响应背景?

yll*_*ate 1 javascript css html5 twitter-bootstrap

我想对网站上的背景进行响应式图像排列.

例如,基于不同的屏幕分辨率,我希望加载不同的背景图像.如果我可以定义不同的行为,那将是有用的; 例如,较高分辨率的桌面将具有较大的拉伸背景图像,而上网本可能具有较小版本的缩小以适应,或者3GS iPhone将具有较小的平铺图像.

你会如何实现这样的场景?

apt*_*tap 5

我相信这会帮助您解决问题.今天我在一个不相关的谷歌考察中遇到了以下图书馆:eCSSential.它看起来像是票.您可以使用库来检测屏幕大小,并加载适当的CSS文件,同时考虑当前视口和当前设备的屏幕大小.很漂亮.

结合我的答案的前一部分,我相信你将有一个非常好的方法来处理你的问题.


以下只是您可以查询的屏幕尺寸的示例,但您明白了.可以为每个方案设置背景样式.

/* 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 */
}

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

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
    /* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
    /* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
    /* Styles */
}
Run Code Online (Sandbox Code Playgroud)

对于任何分辨率,背景可以采用不同的样式,例如:

@media only screen 
and (min-device-width : [width]) 
and (max-device-width : [width]) {

    body {
        background-image: url('../img/image.png');
        -webkit-background-size: cover;
           -moz-background-size: cover;
             -o-background-size: cover;
                background-size: cover;
                // or you could just center the image if it's bigger than the current viewport
                // background-position: center center;
    }
}
Run Code Online (Sandbox Code Playgroud)

信用:大部分来自CSS Tricks