我有一系列滚动水平布局的图像。图像之间有一段距离。我正在使用一个jQuery脚本,负责根据浏览器的窗口大小来调整图像大小。我的问题是,我怎样才能调整图像之间的边距呢?根据浏览器的窗口大小调整边距
我需要的设计是完全流畅的,所以在这种情况下媒体查询不是解决方案。
HTML:
<div id="page">
<div id="header">
</div>
<div id="slides">
<div class="slide"><img src="image01.jpg" /></div>
<div class="slide"><img src="image02.jpg" /></div>
<div class="slide"><img src="image03.jpg" /></div>
....
<div class="slide"><img src="imageN.jpg" /></div>
</div>
<div id="footer">
</div>
</div>
CSS:
#slides {
width: 100%;
white-space: nowrap;
}
.slide {
display: inline-block;
margin-right: 20px;
vertical-align: top;
}
的jQuery:
jQuery(document).ready(function($){
var $window = $(window),
$header = $('#header'),
$footer = $('#footer');
var getHorizontalPageHeight = function() {
return $window.height() - $header.outerHeight() - $footer.outerHeight();
};
var $slides = $('#slides'),
$items = $slides.find('img, iframe');
$items.each(function() {
var $item = $(this),
width = $item.data('width') || $item.attr('width') || 1,
height = $item.data('height') || $item.attr('height') || 1;
$item.data({
height: height,
ratio: width/height
});
});
var resizer = function() {
var contentHeight = getHorizontalPageHeight(),
windowWidth = $window.width(),
windowHeight = $window.height();
$items.each(function() {
var $item = $(this),
originalHeight = $item.data('height'),
height = contentHeight > originalHeight ? originalHeight : contentHeight,
width,
ratio = $item.data('ratio');
width = height * ratio;
$item.css({
width: width,
maxWidth: 'none',
height: width/ratio
});
});
};
$window.on('resize', resizer);
resizer();
});
在此先感谢
你的意思是这样使用'@ media'查询 –
嗨,怎么样让一个固定的宽度和高度为你图片。示例这样的事情。它会根据父div的尺寸自动调整大小。大多数开发者使用这种方法 –
使用CSS(@media) –