简体   繁体   English

使用Twitter Bootstrap的敏感背景?

[英]Responsive backgrounds with Twitter Bootstrap?

I would like to have a responsive image arrangement for backgrounds on a site. 我想对网站背景进行响应式图像排列。

For example, based on different screen resolutions, I would like for different background images to load. 例如,基于不同的屏幕分辨率,我希望加载不同的背景图像。 Further it would be useful if I could define different behaviors; 此外,如果我可以定义不同的行为,这将很有用。 for example, a higher resolution desktop would have a larger stretched background image, while a netbook might have a smaller version of that shrunk to fit, or while a 3GS iPhone would have a small tiled image. 例如,较高分辨率的台式机将具有较大的拉伸背景图像,而上网本可能缩小后的尺寸缩小以适合显示,或者3GS iPhone将具有较小的平铺图像。

How would you implement such a scenario? 您将如何实施这种方案?

I believe this will help you solve your problem. 我相信这将帮助您解决问题。 I came across the following library today on an unrelated Google expedition: eCSSential . 今天,我在一次无关的Google探险中遇到了以下图书馆: eCSSential It looks to be the ticket. 看来是票。 You can use the library to detect screen sizes, and load the appropriate CSS files, taking into consideration the current viewport, and the screen size for the current device. 您可以使用该库来检测屏幕尺寸,并加载适当的CSS文件,同时考虑当前视口和当前设备的屏幕尺寸。 Pretty nifty. 很漂亮

Combined with the previous part of my answer, I believe you will have a pretty good way of dealing with your problem. 结合我的回答的前一部分,我相信您将有一个很好的方式来解决您的问题。


The below is just an example of the screen sizes you can query for, but you get the idea. 以下只是您可以查询的屏幕尺寸的示例,但您了解了。 The background can be styled for each scenario. 可以为每种情况设置背景样式。

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

background can be styled differently for any resolution, for instance: 可以为任何分辨率设置不同的背景样式,例如:

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

Credit: Most of this from CSS Tricks 信用:大部分来自CSS Tricks

You can invest in a little jquery to change your backgrounds based on the screen size. 您可以投资一些jquery来根据屏幕大小更改背景。 Here is an example of what you can do: 这是您可以做什么的示例:

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

Would you mind telling us what you've tried if you have a moment? 如果您有时间,您介意告诉我们您尝试了什么吗?

A few methods are discussed in this article . 本文讨论了几种方法。 The last method may be exactly what you are looking for. 最后一种方法可能正是您要寻找的方法。 It includes a JavaScript function for checking window size and setting the background. 它包括一个JavaScript函数,用于检查窗口大小和设置背景。

You could use the CSS3 Full Page Background Image explained over here on CSS-Tricks , and then go with a jQuery Fallback if necessary. 您可以使用此处在CSS-Tricks上解释过的CSS3整页背景图像,然后在必要时使用jQuery Fallback。 There's a good article for the jQuery method over on Gaya Design Blog Gaya Design Blog上有一篇关于jQuery方法的好文章

CSS: 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;
}

using the @media query method specified by @apttap along with this CSS snippet you could use multiple background images per device pixel ratio. 使用@apttap指定的@media查询方法以及此CSS代码段,您可以为每个设备像素比率使用多个背景图片。

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

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