Skip to content Skip to sidebar Skip to footer

Resize Images In A Horizontal Layout As The Browser Height Changes

I have recently found the following site: https://www.bookworks.org.uk/publishing?content_type[]=output_book&min_price=0 The site uses a horizontal layout instead of a vertical

Solution 1:

I'll answer the question myself. I'm using the following code to make a horizontal layout responsive:

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

            // desktops and tablets (horizontal)
            if (windowWidth > 767) {

                width = height * ratio;

                $item.css({
                    width: width,
                    maxWidth: 'none',
                    height: width / ratio
                });

            // smartphones (vertical)
            } else {

                if ($item.is('iframe')) {
                    $item.css({
                        width: '100%',
                        maxWidth: height * ratio
                    });
                    $item.css('height', $item.width() / ratio);
                } else {
                    $item.css({
                        width: 'auto',
                        maxWidth: '100%',
                        height: 'auto'
                    });
                }

            }

        });

    };

    $window.on('resize', resizer);
    resizer();

});

Solution 2:

I found out that the site is calling a function named resizeTimeline() that uses this:

http://benalman.com/projects/jquery-resize-plugin/

or this:

https://github.com/roxeteer/jquery-afterresize


Post a Comment for "Resize Images In A Horizontal Layout As The Browser Height Changes"