[英]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.