【问题标题】:Alternative for background-size:cover in IE7+在 IE7+ 中替代背景大小:覆盖
【发布时间】:2012-04-25 21:58:32
【问题描述】:

我的网页正文上有一张背景图片。我使用了background-size:cover,以便图像在身体上伸展,同时保持图像的纵横比。我希望 IE7 + IE8 也一样。

我环顾四周,看到了以下代码:

filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
    src='AutumnWinter_4.jpg',
    sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(
    src='AutumnWinter_4.jpg',
    sizingMethod='scale')";

但这并不能保留纵横比,这对我们所针对的网站来说确实很糟糕。

有没有办法做到这一点?不打 jQuery?

【问题讨论】:

  • @MyHeadHurts 这仅适用于 IE9+
  • CSS-Only Technique #1 适用于 IE7,并提供了一个工作示例的链接。尽管“仅 CSS”部分在您添加内联 img 元素时有点误导。据我所知,这将是不“打”JQuery 的唯一方法

标签: html internet-explorer css


【解决方案1】:

只需 CSS,您有两种选择:

  • 使用Object-fit: cover。唯一的问题是它不适用于所有浏览器
  • 如果你想要跨浏览器支持,你可以遵循原始的 CSS 方法:

使用 absolute 将图像放置在容器内,然后使用以下组合将其放置在中心

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

注意: 由于变换 ONLY 适用于 IE9,因此您可以使用过滤器。 Here 是对此的回答。

一旦它在中心,你就可以做到,

// For vertical blocks (i.e., where height is greater than width)
height: 100%;
width: auto;

// For Horizontal blocks (i.e., where width is greater than height)
height: auto;
width: 100%;

这使得图像得到Object-fit:cover的效果。


这里是上述逻辑的演示。

https://jsfiddle.net/furqan_694/s3xLe1gp/

【讨论】:

  • 这在 IE caniuse.com/#feat=transforms2d
  • @Karl-JohanSjögren 添加的注释有帮助吗?
  • 好吧,您的答案仍然没有回答实际问题,因为它只是指另一个答案,该答案最好指向 IE7 的正确方向。此外,您最后一句“此逻辑适用于所有浏览器”仍然是错误的。
  • 没错,我已经删除了最后一句话。感谢您指出。关于我的回答,我仍然希望将其保留在这里,因为它指向一种可能有效的方法。如果我错了,请纠正我@Karl-JohanSjögren
【解决方案2】:

经过多次尝试和错误,最好的解决方案是猜测!

以下内容对我有用。

body {
background-size:100%;
}

【讨论】:

  • 我在问题中提到了这一点。 background-size 在 IE7 中不起作用,所以很遗憾,它并没有真正回答问题。
【解决方案3】:

不幸的是,此类问题的大多数解决方案要么依赖 css3,要么忽略保留图像原始纵横比的“覆盖”的本机功能。 https://github.com/louisremi/background-size-polyfill 应该保持比例,但是当以某些方式拉伸浏览器时,我永远无法让它完全工作(操作员错误,我敢肯定:-))。为了解决这个问题,我编写了一个 jquery 脚本,我已经在 safari、chrome、ff 和 ie8+ 上进行了测试。您会注意到您必须使用绝对定位的 img 而不是 css 背景图像。只需在 html 的标签中添加 bgImg 作为 id。

CSS:

.container { height: auto; overflow:hidden; position:relative;}
.container #bgImg { position:absolute; z-index:-1;} 

您的图像选择器必须绝对定位才能使其位于内容后面。这意味着您的父容器必须具有 position: relative 然后 overflow: hidden 以便从图像中溢出的任何内容(由于您要保持比例,因此不可避免地会隐藏其中的某些部分)。另请注意,父容器中的某些显示标签会破坏溢出的隐藏。

查询:

$(window).load(function () {

    // only do this once and pass as function parameters, because chrome
    // and safari have trouble not only with document.ready but window.resize
    var img = new Image();
    img.src = $("#bgImg").attr('src');
    var $width_orig = img.width;
    var $height_orig = img.height;

    resizeBGImage($width_orig, $height_orig);

    $(window).resize(function () {
        resizeBGImage($width_orig, $height_orig);
    });
});

function resizeBGImage($width_img_orig, $height_img_orig) {
    // get dimensions of container
    var $width_container = $('.container').outerWidth();
    var $height_container = $('.container').outerHeight();

    // calculate original aspect ratio and ratio of the container
    var $imageratio = $width_img_orig / $height_img_orig;
    var $containerratio = $width_container / $height_container;

    var $wdiff = $width_container - $width_img_orig;
    var $hdiff = $height_container - $height_img_orig;

    // original size, so just set to original
    if (($wdiff == 0) && ($hdiff == 0)) {
        $("#bgImg").css('width', $width_img_orig);
        $("#bgImg").css('height', $height_img_orig);
    }
    // if container is smaller along both dimensions than the original image, 
    // set image to container size
    else if (($wdiff < 0) && ($hdiff < 0)) {
        $("#bgImg").css('width', $width_img_orig);
        $("#bgImg").css('height', $height_img_orig+1); // adding one because chrome can't do math
    }
    // if container is wider than long relatiave to original image aspect ratio
    // set width to container width and calculate height 
    else if ($containerratio > $imageratio) {
        $("#bgImg").css('width', $width_container);

        // calculate height using orig aspect ratio and assign to image height 
        $("#bgImg").css('height', (($width_container * $height_img_orig) / $width_img_orig) + 1); // adding one because chrome can't do math
    }
    // else container is taller than long relatiave to original image aspect ratio
    // set height to container height and calculate width
    else {
        // set the image height to container height
        $("#bgImg").css('height', $height_container + 1); // adding one because chrome can't do math

        // calculate width using orig aspect ratio and assign to image width
        $("#bgImg").css('width', (($height_container * $width_img_orig) / $height_img_orig));
    }
    $("#bgImg").css('left', (($width_container - $("#bgImg").width()) / 2).toString() + 'px');
};

注意使用 $(window).load() 而不是 $(document).ready()。 Chrome 和 safari 似乎对后者有问题,因为在这些浏览器中,当 DOM 加载时,背景图像可能没有完全加载。使用 $(window).load() 可确保所有窗口元素在脚本运行之前就位。

【讨论】:

  • 看起来像是一个糟糕的 backstretch.js 版本
【解决方案4】:

我知道这是一个老问题,但我想我会分享一个我想出的解决方案,让其他在谷歌上找到这个问题的人,就像我做的那样。

我试图获取一张图片来覆盖网站的背景并遇到了这个问题,但是没有一个解决方案对我有用。我想出了这个:

HTML:将背景图片移动到&lt;img /&gt;,使其成为&lt;body&gt; 中的第一件事。

<html>
    <body>
        <img class="background" src="kitty.jpg" />
        <div class="content">
            ...

CSS:让背景出现在内容下方,将其min-width/height设置为100%。

html {
    height: 100%
}

body .background {
    position: absolute;
    z-index: -1;
    min-height: 100%;
    min-width: 100%;
}

这里的min-heightmin-width 具有魔力。请勿在 HTML 或 CSS 中为图像指定宽度和高度,否则纵横比会发生变化。

以上内容适用于 IE7IE8。如果你想支持 IE6,你可以像这样设置居中的图片后备:

CSS:如果是 IE6,不显示图片,使用背景图片代替。

body {
    _background: url("kitty.jpg") 50% top no-repeat;
}

body .background {
    _display: none;
}

(注意,如果您不喜欢将下划线 hack 定位到 IE6,您可以改用 conditionals - 这就是 HTML5 Boilerplate 所做的。)

【讨论】:

    【解决方案5】:

    backgroundSize.js 实际上并不会在 IE7 中拉伸 bg 图像,它似乎只是将其以原始大小居中。查看他们的demo 并点击“检查 IE6-7-8 用户通常会看到什么。”


    @danRhul

    我已经读到backstretch 将在 IE7+ 中工作

    祝你好运!

    【讨论】:

      【解决方案6】:

      您可以用实际图像伪造背景图像。它需要更多的 HTML 编辑,当然不理想,但是从什么时候开始处理 IE 变得理想了?

      <body>
        <img id="mainBG" src="mainBG.jpg" />
        <div id="content">
        [ ... ]
      

      然后相应地设置样式

      body{
        position:relative;
      }
      #mainBG{
        width:100%
        position:absolute;
        top:0px;
        left:0px;
      }
      

      如果我没记错的话应该是跨浏览器的。

      【讨论】:

        【解决方案7】:

        我使用了以下 (http://css-tricks.com/perfect-full-page-background-image/),它在 ie7 中运行良好。

        HTML:

        <body>
            <img class="bg" src="filename">       
        </body>   
        

        CSS:

        .bg {
            /* Set rules to fill background */
            min-height: 100%;
            min-width: 1024px;
        
            /* Set up proportionate scaling */
            width: 100%;
            height: auto;
        
            /* Set up positioning */
            position: fixed;
            top: 0;
            left: 0;
        }
        
        @media screen and (max-width: 1024px) { /* Specific to this particular image */
            img.bg {
                left: 50%;
                margin-left: -512px;   /* 50% */
            }
        }
        

        【讨论】:

        • 您的解决方案的一个问题是 IE8 及以下版本不支持您用于“居中”的@media 查询 (max-width: 1024px)。这是我一直在思考解决这个问题的想法时遇到的一个问题。将其居中(如果需要的话)似乎需要 javascript,danRhul 已经将其作为可以接受的答案。
        • 另外,既然你刷了代码(这本身还不错,它显示了研究),你应该提供一个指向源的链接作为信用,无论是here还是其他地方。跨度>
        • 是的,好点 - 我在那里很懒,我在其他地方有它,但没有方便的链接。已编辑
        • 如果图像在整个&lt;body&gt; 上拉伸,有什么方法可以使图像不居中?除了增加负边距,我想。
        【解决方案8】:

        不使用jQuery的原因是什么?您可以仅在IE&lt;8 的条件 cmets 中加载它,这样就不会加载其他所有现代浏览器的 jQuery。

        还要考虑到 IE7 有一个 very low market share (2,52%, April 2012),因此如果此功能对您的站点/应用程序非常重要,则可以为该特定浏览器加载大约 25kb 的额外空间是可以接受的。

        所以,我找到了这个 jQuery 插件:https://github.com/louisremi/jquery.backgroundSize.js

        一个 jQuery cssHook 为 IE6-7-8 添加了对“cover”和“contain”的支持,在 1.5K 中

        请参阅Github project page 了解更多信息。

        【讨论】:

        • 我想不使用 jQuery 的原因是因为 stackoverflow.com/questions/9478737/…
        • 一些想法: 1. 我认为这里需要 js 2. 技巧只适用于 IE(通常 IE 自带 js,不包括内网) 3. 考虑渐进增强。没有 JS -> 您的图像将被定位(在 IE 中)看到不同的效果。
        • 不使用 Jquery 的另一个原因是我们可能需要相同的电子邮件模板。大多数电子邮件客户端,例如 Gmail 和 Outlook(大的)都会删除任何 JS。甚至 CSS 也需要过时 -- Outlook 2010 仍然基于 IE6 或更低版本。
        • @PKHunter,Outlook 2010 及更高版本使用 Word 来呈现 HTML,而不是 IE6。可以说这更糟。
        【解决方案9】:

        听起来您需要像 Modernizer 这样的“垫片”或“polyfill”: http://modernizr.com/docs/#html5inie

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多