根据浏览器窗口大小调整边距

lee*_*mon 0 css jquery margin horizontal-scrolling image-resizing

我有一系列滚动水平布局的图像。图像之间有边距。我正在使用 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>
Run Code Online (Sandbox Code Playgroud)

CSS:

#slides {
    width: 100%;
    white-space: nowrap;
}

.slide {
    display: inline-block;
    margin-right: 20px;
    vertical-align: top;
}
Run Code Online (Sandbox Code Playgroud)

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();

});
Run Code Online (Sandbox Code Playgroud)

提前致谢

Mih*_*i T 5

如果您不想使用 mediaQ 您可以使用百分比作为margin-right:2%。这 2%取决于调整窗口大小(随着窗口变小,窗口大小也会变小)

看这里 jsfiddle 宽度百分比

代码 :

 .slide {
display: inline-block;
margin-right: 2%;
vertical-align: top;
height:100px;
background:red;
width:20%
}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用vw这意味着视口(窗口)宽度。其中100vwmax 和0vwmin 。同样,它margin-right:2vw会根据窗口的宽度而增加或减少。

看到这里jsfiddle 与大众

代码 :

.slide {
display: inline-block;
margin-right: 2vw;
vertical-align: top;
height:100px;
background:red;
width:20%
}
Run Code Online (Sandbox Code Playgroud)

让我知道这两种解决方案之一是否适合您。

PS:我把它width和仅height用于.slide示例目的