简体   繁体   English

固定拉伸背景图片

[英]Fixed stretching background image

I have drafted up a brief example of what I have so far, and what I'm trying to achieve. 我已经草拟了一个简短的示例,说明了到目前为止的工作以及我要实现的目标。

See my demo here 在这里查看我的演示

In my jsfiddle you will see a wrapping div named "content-wrap". 在我的jsfiddle中,您将看到一个名为“ content-wrap”的包装div。 Within this div I want an image that can stretch to window size and content. 在这个div中,我希望图像可以扩展到窗口大小和内容。

I have done background image stretching before: See example here 我之前做过背景图片拉伸:请参见此处的示例

But this is a bit more complicated because it is for a specific region within the site. 但这有点复杂,因为它是针对站点内的特定区域的。

I have tried having a play so far with my existing code from the link above and I think I may need to take a different approach. 到目前为止,我已经尝试使用上面链接中的现有代码进行测试,我认为我可能需要采用其他方法。 As when you scroll down the images doesn't stay in position it moves with the scroll bar (as it should). 当您向下滚动图像时,图像不会停留在原位,而是随滚动条一起移动(应如此)。

Any ideas? 有任何想法吗?

If you don't mind using CSS3's background-size property, then the following additions to your #content-wrap { CSS statements will achieve a responsive background image that will expand to fit the size of the container. 如果您不介意使用CSS3的background-size属性,则在#content-wrap { CSS语句中添加以下#content-wrap {将获得可响应的背景图像,该图像将扩展为适合容器的大小。

#content-wrap {
    height: 1000px;
    background: #ccc;
    color: #fff;

    /* Added CSS */

    display: block;
    max-width: 100%;
    clear: both;
    background: transparent url("http://placekitten.com/900/900") top right no-repeat;
    background-size: cover;
}

Here is an updated version of your jsfiddle with my added CSS and some lovely placekittens: http://jsfiddle.net/kztGj/22/ 这是您的jsfiddle的更新版本,其中包含我添加的CSS和一些可爱的小猫: http : //jsfiddle.net/kztGj/22/

The downsides to this are of course that it will not work in Internet Explorer < 9, Firefox 3.6, or any browser that doesn't support CSS3. 不利之处当然是它不能在Internet Explorer <9,Firefox 3.6或任何不支持CSS3的浏览器中使用。 But then your asking for a responsive image, and as such you'll be hard pushed to find an elegant solution that supports older browsers. 但是随后您需要一个响应式图像,因此,您将很难找到一种支持较旧浏览器的优雅解决方案。

For more information on the background-size property and its uses, I recommend checking out this article , its quite informative. 有关background-size属性及其用途的更多信息,我建议您查阅这篇非常有用的文章

In your image, add a css like this 在您的图像中,添加这样的CSS

.custom-image {
    width:100%;
}

and your html 和你的HTML

<div>
    <img src="" class="custom-image" />
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM