2015-04-12 58 views
1

我想放置一個響應絕對定位的圖像內的相對股利。我已經將邊界放在兩個潛水位置,以便將問題可視化。右邊的那個不包裝。此外,我想保持圖像導航在圖像中間。relative div並未包含其絕對內容......任何人都知道解決方案?

主要目標是保持包裝div響應和包裝圖像的大小 - 當窗口大小改變圖像大小時。

在這裏你可以看到問題codepen - http://codepen.io/GlupiJas/pen/PwrLbb


enter image description here

advert.css

body{ 

} 
.wrapper{ 
    width: 80%; 
    margin: 100px auto; 
} 
.advert{ 
    position: relative; 
    height: 100%; 
} 
.advert img{ 
    position: absolute; 
} 

.advert button{ 
    position: absolute; 
    visibility: hidden; 
} 
.advert:hover button{ 
    visibility: visible; 
} 

.advert button#advertCloseButton{ 
    top: 0px; 
    right: 0px; 
} 
.advert button#advertNextButton{ 
    bottom:50%; 
    left:0px; 
} 
.advert button#advertPreviousButton{ 
    bottom:50%; 
    right:0px; 
} 
.advert button#advertStopButton{ 
    bottom:0px; 
    right:0px; 
} 
.advert button#advertPlayButton{ 
    bottom:0px; 
    right:0px; 
} 

.border{ 
    border: 1px solid gray; 
} 

的index.html

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Title of the document</title> 
    <!-- jQuery --> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
    <!-- Bootstarp 3 Latest compiled and minified CSS --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
    <!-- Bootstarp 3 Optional theme --> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"> 
    <!-- Bootstarp 3 Latest compiled and minified JavaScript --> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
    <!-- custom css --> 
    <link rel="stylesheet" type="text/css" href="advert.css"> 
    <script src="js/advert.js"></script> 
</head> 

<body> 
    <div class="wrapper"> 
    <div class="row"> 
     <div class="col-xs-12 col-md-8 border"> 
     .col-xs-12 .col-md-8 
     </div> 
     <div class="col-xs-12 col-md-4 border"> 
     <div class="advert"> 
      <img class="img-responsive" src="http://placehold.it/300x300.jpg" alt="Smiley face" height="" width="100%"> 
      <img class="img-responsive" src="http://placehold.it/300x300.jpg" alt="Smiley face" height="" width="100%"> 
      <img class="img-responsive" src="http://placehold.it/300x300.jpg" alt="Smiley face" height="" width="100%"> 
      <button id="advertCloseButton" type="button" class="btn btn-danger">X</button> 
      <button id="advertNextButton" type="button" class="btn btn-danger">></button> 
      <button id="advertPreviousButton" type="button" class="btn btn-danger"><</button> 
      <button id="advertStopButton" type="button" class="btn btn-danger">pause</button> 
      <button id="advertPlayButton" type="button" class="btn btn-danger">play</button> 
     </div> 
     </div> 
    </div> 
    </div> 
</body> 

<script> 
$(function(){ 
    advert({ 
     closeButton: true, 
     navButton: true, 
     timeToShow: 100, 
     timeDelay: 1000, 
     timeToFade: 100 
    }); 
}); 
</script> 

</html> 
+0

當你絕對位置的元素,它被取出來了公文流轉,並不會影響它的父(S)的尺寸。也許絕對定位不是你應該考慮實現這種特定佈局的東西? – Terry

+0

也許或...我可以使用javascript/jQuery來確定它在一些interwal中的實際大小,它將繼續檢查圖像變化的大小,並分別更改包裝div的大小? – DevWL

+0

這是可能的,但您可能想重新考慮您實際想要通過佈局實現的目標。您是否在嘗試爲廣告創建一個排序圖像滑塊?使用JS是可能的,但不必要的複雜,因爲記住你的圖像尺寸是可以響應的,所以你每次重新調整視口時都必須重新計算高度。 – Terry

回答

0

我很快嘲笑了一個演示,但你遇到的問題更多是因爲你有很多div添加(一個添加div爲邊界和其他一些不需要的 - 因此我必須設置高度:100%到它們中的每一個)並且沒有爲所有圖像和按鈕的容器設置高度。

因此,確保position:relative;父母和position:absolute;它的孩子將工作,如果容器有一個高度設置,並將確保您的播放按鈕包含在同一個div。

在這個演示中,我迅速將高度設置爲所有父容器的高度:100%;向您展示高度如何影響問題。

注意要有height:100%工作,然後每個父母需要有一個高度固定設置或每個父母從html開始,身體需要有100%的身高。

如果您爲容器添加固定高度,或者將html,body設置爲100%高度,然後確保所有兒童都有100%高度,則可以解決您的問題。

DEMO http://codepen.io/alexincarnati/pen/RNzvmw

+0

Thx的代碼,但在你的解決方案後,調整窗口按鈕和導航大小不堅持圖像,但留在相同的positioin在包裝它的大小。容器也必須對圖像大小作出響應。 – DevWL

+0

不客氣。請記住,構建完全可行的完全解決方案可能需要時間。有幾種方法取決於您想要容納什麼樣的內容。例如,如果您需要保持img的寬高比(如果img不是方形)。我只是指出了一種方法,然後您可以通過媒體查詢來修復它,並檢查每個列的寬度,因爲您使用的是bootstrap ... –

+0

我同意。我想讓它的工作與內容大小無關。看看如果圖像大小是隨機的,它會是什麼樣子 http://codepen.io/anon/pen/emwxqr – DevWL

相關問題