正如其他人所说,使用absolute
定位你的图像是没有意义的 - 它会使事情更难。但是,您可以使用absolute
元素可以将置于之后的其他元素,而不用推送内容。我的建议是只有一个班级能够告诉你当前正在显示哪个图片。显示的图像将有一个relative
的位置,其他将有一个absolute
的位置,允许图像出现在左上角相同的位置,但一些被遮挡而另一些可见。
下面的方法的主要优点是您的图像可以根据您的需要调整您的包装盒的尺寸,并且photoset
可以作为覆盖层等内嵌放置在文档流中......它非常灵活,所以给它一试。这也意味着你的JavaScript变得更轻松,更容易,因为你需要做的就是将正确的类添加到图像来显示它。您还可以添加一个带有标题的相对框,并且它可以正常工作。
// Don't worry about this, its just a quick implementation to make your photos rotate
var ps = Array.prototype.slice.call(document.querySelectorAll('.photoset')).forEach(function(set){
var all = Array.prototype.slice.call(set.querySelectorAll('img'));
var index = 0;
setInterval(function(){
all[index].className = '';
index = index + 1 >= all.length ? 0 : index + 1;
all[index].className = 'active';
},1000)
});
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
.body {
height: 100%;
width: 100%;
}
.photoset {
box-shadow: 0px 0px 25px -2px rgba(89,89,89,1);
overflow: hidden;
width: auto;
display: inline-block;
}
#absolute-photoset {
position: absolute;
left: 50%;
top: 50%;
max-width: 80%;
max-height: 80%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
z-index: 1;
}
.photoset img {
position: absolute;
left: 0;
top: 0;
max-width: 100%;
max-height: 100%;
opacity: 0;
display: block;
}
.photoset img.active {
position: relative;
opacity: 1;
}
<div class="photoset" id="absolute-photoset">
<img src="http://inspirebee.com/wp-content/uploads/2013/04/animal-fashion-parade.jpg" class="active" />
<img src="http://www.fubiz.net/wp-content/uploads/2013/03/Fashion-Zoo-Animals18.jpg" />
<img src="http://inspirebee.com/wp-content/uploads/2013/04/animal-in-fashion.jpg" />
<img src="http://www.fubiz.net/wp-content/uploads/2013/03/Fashion-Zoo-Animals20.jpg" />
</div>
<h1>My Title</h1>
<div class="photoset" id="inline-photoset">
<img src="http://inspirebee.com/wp-content/uploads/2013/04/animal-fashion-parade.jpg" class="active" />
<img src="http://www.fubiz.net/wp-content/uploads/2013/03/Fashion-Zoo-Animals18.jpg" />
<img src="http://inspirebee.com/wp-content/uploads/2013/04/animal-in-fashion.jpg" />
<img src="http://www.fubiz.net/wp-content/uploads/2013/03/Fashion-Zoo-Animals20.jpg" />
</div>
<p>The above photoset seems to be pushing this text down as expected!</p>
这photoset类也推内容出它的方式,如果它是不定位绝对的,我的猜测是,你在找什么。你也可以调整.photoset
的大小,并且里面的图像应该跟随(我不确定max-width
是否会改变图像的比例,如果是这样,请只使用单向max
值,如只是max-width
)。
主要优点是可以治疗.photoset
作为一个盒子,不用担心它的内容,所以现在你可以在线使用,绝对定位,固定等..
绝对定位是一个**布置网页的方法非常糟糕。它非常不灵活,并且有更好更快的响应选项。看看[** LearnLayout.com **](http://learnlayout.com/) –