2015-04-12 54 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

相关问题