而不是使用:last-child
的,你可以使用:first-child
和交換保證金適合利潤率左
這將是這樣的:
.thumbnail {
border:1px red solid;
margin-left:15px;
}
.slide:first-child .thumbnail {
margin-left: 0;
}
如果你不擔心IE7或更低版本,您可以通過將box-sizing
屬性設置爲border-box
來跳過添加.thumbnail
altogther。
.slide {
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* not sure if the prefixes are still needed */
margin-left: 15px;
}
.slide:first-child{
margin-left: 0;
}
框大小控制邊距和填充如何影響元素的寬度。默認值爲content-box
,它的工作方式與以前一樣:一個元素有一個寬度,比如說100px,邊距和填充距離就是這個寬度。所以,如果你有10px的保證金和5像素填充你的寬度+保證金+填充將等於130
當屬性設置爲border-box
,寬度包括 margin和padding。因此,100px寬度+ 10px + 5px填充將具有... 100px的總寬度!換句話說,邊距和填充會擠壓內容,而不是向外推動更多空間。克里斯Coyier作爲great article和an example看到它的行動。
編輯:小提琴使用:first-child
:http://jsfiddle.net/Ws4UQ/2/
相關代碼:
#wrapper {
position:relative;
left:0;
width:1300px;
margin-left:-115px; /* This is the width + margin of .slide */
}
.slide {
width:100px;
float:left;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* not sure if the prefixes are still needed */
margin-left: 15px;
}
.slide:first-child {
margin-left: 0;
}
保證金只需要滑動轉盤(#wrapper
)時予以考慮。而不是將其移動-100像素,它將是-115像素(額外的15像素是無論您設置邊距)。只要知道,如果你需要它在IE8中工作,你將不得不創建一個polyfill(more info from Chris again)。
謝謝。在我的jsfiddle中,包含文本「2」的幻燈片位於位置1.含有「1」的幻燈片位於-1(不可見)。一旦用戶推進傳送帶,任何'* -child'風格選擇器將不再匹配適當的元素。 我對使用JS添加/刪除類的擔心會是突然跳過的元素。有一個按鈕可以使旋轉木馬前進一個滑動另一個以前進一個頁面(5個幻燈片),並且當前進5個幻燈片時動畫會播放更長時間......因此動態添加/刪除填充並且不會搞亂這些複雜的動畫一個壯舉 –
跑出評論空間,但我想補充一點。我在你的小提琴中編輯第22行以模擬用戶推進幻燈片,以說明問題 - http://jsfiddle.net/c34CF/7/你會看到現在有一個差距,因爲樣式匹配錯誤的元素。它與最後一個元素相匹配,但不再是最後一個元素。 –