繁体   English   中英

使用Twitter Bootstrap的敏感背景?

[英]Responsive backgrounds with Twitter Bootstrap?

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

例如,基于不同的屏幕分辨率,我希望加载不同的背景图像。 此外,如果我可以定义不同的行为,这将很有用。 例如,较高分辨率的台式机将具有较大的拉伸背景图像,而上网本可能缩小后的尺寸缩小以适合显示,或者3GS iPhone将具有较小的平铺图像。

您将如何实施这种方案?

我相信这将帮助您解决问题。 今天,我在一次无关的Google探险中遇到了以下图书馆: 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 */
}

可以为任何分辨率设置不同的背景样式,例如:

@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;
    }
}

信用:大部分来自CSS Tricks

您可以投资一些jquery来根据屏幕大小更改背景。 这是您可以做什么的示例:

if ( $(window).width() < 1000 ) {
    $('body').css('background','url("yourimage")';
} else {
    $('body').css('background','url("yourimage")';
}

如果您有时间,您介意告诉我们您尝试了什么吗?

本文讨论了几种方法。 最后一种方法可能正是您要寻找的方法。 它包括一个JavaScript函数,用于检查窗口大小和设置背景。

您可以使用此处在CSS-Tricks上解释过的CSS3整页背景图像,然后在必要时使用jQuery Fallback。 Gaya Design Blog上有一篇关于jQuery方法的好文章

CSS:

html { 
    background: url(images/bg.jpg) no-repeat center center fixed; 
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

使用@apttap指定的@media查询方法以及此CSS代码段,您可以为每个设备像素比率使用多个背景图片。

暂无
暂无

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

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