HTML/CSS:IMG 的高度等于文档的高度(不是窗口)

发布于 2024-10-11 20:47:47 字数 21 浏览 2 评论 0原文

我怎样才能达到这样的效果呢?

How can I achieve that effect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

权谋诡计 2024-10-18 20:47:47

对于完整的水平和垂直尝试如下:

background-image: url(image.jpg);
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed;
-o-background-size: 100% 100%, auto;
-moz-background-size: 100% 100%, auto;
-webkit-background-size: 100% 100%, auto;
background-size: 100% 100%, auto;

或者更好地将您的代码发布在 http://jsfiddle.net/

For full horizontal and vertical try something like:

background-image: url(image.jpg);
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed;
-o-background-size: 100% 100%, auto;
-moz-background-size: 100% 100%, auto;
-webkit-background-size: 100% 100%, auto;
background-size: 100% 100%, auto;

or better post a your code in a http://jsfiddle.net/

自在安然 2024-10-18 20:47:47

您的问题很可能源于 body 标签是静态定位的,因此其子标签的高度是相对于 html 标签的。

尝试添加此内容:

body{
  position:relative;
}

它将使正文的子级使用文档的大小而不是浏览器视口。

这是它的工作示例: http://jsfiddle.net/cP9hu/

Your problem most likely stems from the fact that the body tag is positioned statically, so heights of its children are relative to the html tag.

Try adding this:

body{
  position:relative;
}

It will make the body's children use the document's size rather than the browser viewport.

Here is an example of it working: http://jsfiddle.net/cP9hu/

吲‖鸣 2024-10-18 20:47:47

如果有一个原因这不起作用:

CSS Code

    #theImage {
        height: 100%;
    }

然后使用 jQuery 你可以这样做:

    var docHeight = $(document).height();
    $('#theImage').css('height', docHeight);

我不知道如何在普通的旧 JavaScript 中执行此操作,但这就是 jQuery 示例。

If there's a reason that this doesn't work:

CSS Code

    #theImage {
        height: 100%;
    }

Then with jQuery you could do this:

    var docHeight = $(document).height();
    $('#theImage').css('height', docHeight);

I'm not sure how to do this in plain old JavaScript, but that's the jQuery example.

酒与心事 2024-10-18 20:47:47

这听起来像是必须通过 JavaScript 处理的事情。就我个人而言,我会使用 jQuery 和一些效果:

$(document).ready(function() { //Runs at page load
    $(".ID_OF_IMAGE").each(function() { //calls the anon function for the id tag specified
        /*sets the height to the max of three methods of height measurement
        diff browsers detect doc height differently.*/
        this.height = Math.max(
            //gets the larger between doc height and body height
            Math.max(document.body.clientHeight, document.documentElement.clientHeight),
            Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
            Math.max(document.body.scrollHeight, document.documentElement.scrollHeight))
    });
});

或者,您可以将图像设置为 div 或正文的背景,并使用 Repeat-y css 属性。

body { //also works for div's
    background-image:url('image.jpg');
    background-repeat:repeat-y;
}

-SB

That sounds like something that might have to be handled via JavaScript. Personally I would use jQuery and something to the effect of:

$(document).ready(function() { //Runs at page load
    $(".ID_OF_IMAGE").each(function() { //calls the anon function for the id tag specified
        /*sets the height to the max of three methods of height measurement
        diff browsers detect doc height differently.*/
        this.height = Math.max(
            //gets the larger between doc height and body height
            Math.max(document.body.clientHeight, document.documentElement.clientHeight),
            Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
            Math.max(document.body.scrollHeight, document.documentElement.scrollHeight))
    });
});

Alternately you can set the image as a background of a div or the body and use the repeat-y css property.

body { //also works for div's
    background-image:url('image.jpg');
    background-repeat:repeat-y;
}

-SB

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文