2013-03-09 66 views
0

我試圖將所有隨機定位的圖像包含在它的100%包裝中,圖像在頁面加載時隨機定位,但是由於我已將套餐設置爲溢出:隱藏了一些圖像被切斷。是否有可能在包裝中包含所有圖像,同時也不會重疊圖像?
這裏有一個的jsfiddle:http://jsfiddle.net/mdxZL/2/
HTML:css:在瀏覽器中包含隨機定位的圖像

<div id="wrap" style="width: 100%; height: auto; position: absolute; left: 0; top: 0; overflow: hidden;"> 

<div id="images"> 
    <img src="http://placekitten.com/200/200" /> 
    <img src="http://placekitten.com/200/200" /> 
    <img src="http://placekitten.com/200/200" /> 
    <img src="http://placekitten.com/200/200" /> 
    <img src="http://placekitten.com/200/200" /> 
</div> 
</div> 

CSS:

#images 
    { 
     left: 0; top: 0; 
     width: 100%; 
    } 

    #images img 
    { 
     padding: 10px; 
     position:relative; 
} 

的jQuery:

$(document).ready(function(){ 
$("#images img").each(
function(intIndex) { 

var l = Math.floor(Math.random() * $("#images").width()); 
var t = Math.floor(Math.random() * $("#images").height()); 

$(this).css("margin-left", l); 
$(this).css("margin-top", t); 

} 

); 
}); 
+0

您需要添加一個檢查以確保[left + img.width]小於父代的計算寬度,而[top + img.height]與父代的計算高度相同。 – cimmanon 2013-03-09 16:12:26

回答

0

嘗試

#images img 
{ 
    padding: 10px; 
    position:relative; 
    width:100%; 
} 
+0

這只是使圖像100%的寬度,我希望圖像保持在200px,但保持包含在它的父div的邊界內(所以你沒有被顯示一半的圖像) – user1374796 2013-03-09 16:12:01