根据浏览器的 window 大小调整边距
Adjust margins depending on browser's window size
我有一系列水平滚动布局的图像。图像之间有一个边距。我正在使用 jQuery 脚本,该脚本负责根据浏览器的 window 大小调整图像大小。我的问题是,如何调整图像之间的边距?
我需要设计完全流畅,因此在这种情况下媒体查询不是解决方案。
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();
});
提前致谢
使用媒体查询
/*========== Mobile First Method ==========*/
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {
}
/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {
}
如果您不想使用 mediaQ,您可以将百分比用作 margin-right:2%
。 2%
取决于调整 window 的大小(它会随着 window 变小而变小)
代码:
.slide {
display: inline-block;
margin-right: 2%;
vertical-align: top;
height:100px;
background:red;
width:20%
}
或者您可以使用 vw
表示视口 (window) 宽度。其中 100vw
是 max 和 0vw
min 。同样,margin-right:2vw
将根据 window 的宽度增加或减少。
代码:
.slide {
display: inline-block;
margin-right: 2vw;
vertical-align: top;
height:100px;
background:red;
width:20%
}
让我知道这两种解决方案中的一种是否适合您。
PS : 我把 width
和 height
放到 .slide
中只是为了举例
我有一系列水平滚动布局的图像。图像之间有一个边距。我正在使用 jQuery 脚本,该脚本负责根据浏览器的 window 大小调整图像大小。我的问题是,如何调整图像之间的边距?
我需要设计完全流畅,因此在这种情况下媒体查询不是解决方案。
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();
});
提前致谢
使用媒体查询
/*========== Mobile First Method ==========*/
/* Custom, iPhone Retina */
@media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
@media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
@media only screen and (min-width : 768px) {
}
/* Medium Devices, Desktops */
@media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
@media only screen and (min-width : 1200px) {
}
如果您不想使用 mediaQ,您可以将百分比用作 margin-right:2%
。 2%
取决于调整 window 的大小(它会随着 window 变小而变小)
代码:
.slide {
display: inline-block;
margin-right: 2%;
vertical-align: top;
height:100px;
background:red;
width:20%
}
或者您可以使用 vw
表示视口 (window) 宽度。其中 100vw
是 max 和 0vw
min 。同样,margin-right:2vw
将根据 window 的宽度增加或减少。
代码:
.slide {
display: inline-block;
margin-right: 2vw;
vertical-align: top;
height:100px;
background:red;
width:20%
}
让我知道这两种解决方案中的一种是否适合您。
PS : 我把 width
和 height
放到 .slide
中只是为了举例