此页面 http://www.erica2013.dreamhosters.com/(使用 Zurb 基础框架构建)宽度超过 425 像素,在带有书架背景图像的 div 上有一排书。
在手机或浏览器中,当宽度小于 425 像素时,不使用背景,内部 div 变为行。
CSS
#homeBookshelfRow {
margin-top:50px;
background:url(../img/homepage/hmpg-mini-shelf/hmpg-shelf.png) bottom center no-repeat;
height:160px;
padding:19px 0 0 11px;
text-align:center;
}
#homeBookshelfHeadline {
display:none;
}
.phoneBookGroup {
margin:0;
padding:0;
display:inline;
}
#homeBookshelfRow .phoneBookGroup .homebook {
padding-right:10px;
cursor:pointer;
}
基于宽度的 CSS
@media only screen and (max-width: 425px) {
#homeBookshelfRow {
background:none;
height:auto;
margin:0 0 20px 0;
}
#homeBookshelfHeadline {
display:block;
font-family: 'Open Sans Condensed', sans-serif;
font-size:2.25em;
color:#b11717;
padding:0;
margin:0;
}
.phoneBookGroup {
display:block;
margin-bottom:10px;
}
}
HTML
<div class="row" id="homeBookshelfRow">
<p id="homeBookshelfHeadline">
Bookshelf
</p>
<div class="phoneBookGroup">
<img src="/bookshelf/HCcovers/21_199.jpg" alt="Justice for Sara" width="88" height="141" rel="21" data-urlname="Justice-for-Sara" class="homebook">
<img src="/bookshelf/HCcovers/22_199.jpg" alt="Storm Season" width="88" height="141" rel="22" data-urlname="Storm-Season" class="homebook">
<img src="/bookshelf/HCcovers/23_199.jpg" alt="Wishing Moon" width="88" height="141" rel="23" data-urlname="wishing-moon" class="homebook"></div>
<div class="phoneBookGroup">
<img src="/bookshelf/HCcovers/18_199.jpg" alt="Watch Me Die" width="88" height="141" rel="18" data-urlname="Watch-Me-Die" class="homebook">
<img src="/bookshelf/HCcovers/19_199.jpg" alt="Slices of Night" width="88" height="141" rel="19" data-urlname="Slices-of-Night" class="homebook">
<img src="/bookshelf/HCcovers/17_199.jpg" alt="Blood Vines" width="88" height="141" rel="17" data-urlname="blood-vines" class="homebook"></div>
<div class="phoneBookGroup">
<img src="/bookshelf/HCcovers/16_199.jpg" alt="Breakneck" width="88" height="141" rel="16" data-urlname="breakneck" class="homebook">
<img src="/bookshelf/HCcovers/24_199.jpg" alt="Chances Are" width="88" height="141" rel="24" data-urlname="chances-are" class="homebook">
</div>
</div>
它最初呈现良好,但以下 AJAX 调用更改了大书图像和侧边栏内容。当在所有测试过的浏览器中发生这种情况时,不再呈现基于宽度的 CSS,而是呈现常规 CSS。
为什么会这样?我如何解决它?
$('.homebook').click(function() {
var id = $(this).attr('rel');
var urlname = $(this).attr('data-urlname');
//console.log("Book id " + id);
$('#bigHomeBook').attr( 'src' , '/bookshelf/hmpg-mini-shelf/large/'+id+'.jpg' );
$('#bigHomeBookLink').attr( 'href' , '/bookshelf/' + urlname );
$.ajax({
url: 'inc/home_book_ajax.php?bID=' + id,
success: function(data) {
if (data == 'bad') {
} else {
$('#rightSidebar').html( '' );
$('#rightSidebar').addClass( 'reviewRed' );
$('#rightSidebar').html( data );
}
}
});
});