【问题标题】:Responsive background with maintained aspect ratio保持宽高比的响应式背景
【发布时间】:2013-03-22 17:31:17
【问题描述】:

我正在使用 Malsup 的 Cycle 2 创建背景幻灯片,在单独的 div 中带有相应的滑动文本。我在这里有一些简单的标记,但似乎无法裁剪图像,以便它始终是浏览器高度的 100%(如果你制作一个薄窗口,你会看到底部的红色)。

也许解决方案是 jQuery 或 CSS — 我看到的所有内容都建议在图像和父 div 上使用 height:auto,但无济于事。

jsfiddle

<div id="background" class="cycle-slideshow" 
                     data-cycle-fx="scrollHorz" 
                     data-cycle-timeout="2000"
                     data-cycle-slides="> div"
>
    <div style="background:#fcc">
        <img src="http://stoptraining.me/staged/IMG_1402.jpg">
    </div>
    <div style="background:#cfc">
        <img src="http://stoptraining.me/staged/IMG_1403.jpg">
    </div>
</div>

<div class="center">
    <div id="text" class="cycle-slideshow" 
                   data-cycle-fx="fade" 
                   data-cycle-timeout="2000"
                   data-cycle-slides="> div"
    >
        <div>
            <h2>Title</h2>
            <p>Lorem ipsum dolor ...</p>
        </div>
        <div>
            <p>Mel eu pertinax ...
        </div>
        <div>
            <p>Utinam electram pertinacia ...
        </div>
    </div>
</div>

CSS:

body, html {
    background: red;
    padding: 0;
    margin: 0;
    height: auto;
    width: 100%;
}
#background {
    position: fixed;
    width: 100%;
    height: auto;
    z-index: 99;
    overflow: hidden;
}
#background div {
    width: 100%;
    height: auto;
    background-size: cover;
    overflow: hidden;
}
#background div img {
}
img {
    width: 100%;
    height: auto;
}
#text {
    position: relative;
    text-align: center;
    z-index: 99;
}
.center {
    background: white;
    padding: 200px 0 0 0;
    width: 400px;
    overflow: auto;
    min-height: 1000px;
    margin: auto;
    z-index: 9999;
    position: relative;
    text-align: center;
}

【问题讨论】:

    标签: jquery responsive-design slideshow cycle jquery-cycle


    【解决方案1】:

    目前没有任何 CSS 样式可以帮助任何元素根据哪一侧更大来保持其纵横比;因此,您将不得不求助于使用 JavaScript。

    这是一个有效的jsFiddle,它完成了你想要的。


    上述解决方案包含对您原来的一些修改...

    CSS:

    删除:

    #background div img {
    }
    img{
        width: 100%;
        height: auto;
    }
    

    更改:

    #background {
        /* ... */
        /* make sure width and height are 100%! */
        width: 100%;
        height: 100%;
        /* ... */
    }
    #background div {
        /* ... */
        /* make sure width and height are 100%! */
        width: 100%;
        height: 100%;
        /* ... */
    }
    

    添加:

    #background div img.wider {
        width: 100%;
        height: auto;
    }
    #background div img.higher {
        width: auto;
        height: 100%;
    }
    

    JavaScript:

    var imgWidth = 1600;
    var imgHeight = 1067;
    var imgRatio = imgWidth / imgHeight;
    
    $(ResizeBackground); // call on initialize
    $(window).resize(ResizeBackground); // and on resize
    
    function ResizeBackground() {
        var div = $('#background');
        var divWidth = div.width();
        var divHeight = div.height();
    
        // if the div's ratio is higher than the image, it means that
        // the div is wider than the image (and the background will be seen on the sides).
        // Else, the div is higher and will display the background on the bottom.
    
        // this condition will change the images proportion to always fill the background.
        if (divWidth / divHeight > imgRatio) {
            div.find('img').removeClass('higher').addClass('wider');
        } else {
            div.find('img').removeClass('wider').addClass('higher');
        }
    }
    

    更新

    为了使图像居中同时按比例调整大小,您必须使用以下计算:

    var $imgs = div.find('img');
    
    if (divRatio > imgRatio) {
        $imgs.each(function () {
            var $this = $(this);
            // set a negative top margin (and move the image up)
            $this.css('margin-top', -Math.abs(div.height() - $this.outerHeight()) / 2);
        });
    } else {
        $imgs.each(function () {
            var $this = $(this);
            // set a negative left margin (and move the image to the left)
            $this.css('margin-left', -Math.abs(div.width() - $this.outerWidth()) / 2);
        });
    }
    

    我稍微更新了jsFiddle,请看一下// NEW!!the old one的区别。

    【讨论】:

    • 一旦小于 WebKit 中的原始图像大小,这似乎不会调整大小。有什么解决办法吗?
    • 也许我不明白你在经历什么——我刚刚在Chrome 26.0.1410.43 mInternet Explorer 10 中尝试了jsFiddle above,我发现它按预期工作。你能详细说明一下吗?
    • 我在这里犯了一个错误,在这方面工作正常。但是,如果裁剪,有没有办法让图像居中?现在 - 当使用长的垂直窗口进行裁剪时,图像向左对齐,并且图像的中心隐藏在屏幕右侧之外。
    • 非常感谢。理想情况下,它会显示尽可能多的图像(始终显示 100% 的高度,并裁剪掉 & 居中的水平溢出。
    • @RooWM 我已经更新了我的答案。请查看 Update 部分。
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多