简体   繁体   English

如何将内容放置在固定图像下方?

[英]How to place content below a fixed image?

JSFiddle: JSFiddle:

http://jsfiddle.net/qd8Ltufw/6/ http://jsfiddle.net/qd8Ltufw/6/

I have a page with a background image and content centered over the image. 我有一个页面,上面有背景图片,内容居中。 That's all working fine and I have it setup how I need it, but now I need to add content below the image. 一切正常,我已经按照需要设置了它,但是现在我需要在图像下方添加内容。

CSS for image: 图片的CSS:

img.home-bg {
  min-height: 100%;
  min-width: 1024px;
  width: 100%;
  height: auto;
  position: fixed;
  top: 0;
  left: 0;
  margin-top: 100px;
}

You can see from the fiddle that when I add any content below the div with the image in it doesn't show below the image. 您可以从小提琴中看到,当我在带有图片的div以下添加任何内容时,该图片不会显示在图片下方。 It shows either above it or underneath it. 它显示在其上方或下方。 I need to be able to place content under the image that the user can scroll down to see. 我需要能够将内容放置在用户可以向下滚动查看的图像下方。

Sorry I could not post under your post, due to low rep. 抱歉,由于回复率低,我无法在您的帖子下发帖。

Please see this link and tell me what you think, does this answer your question? 请查看此链接并告诉我您的想法,这是否回答了您的问题? Code

$(document).ready(function(){
    $(".home-callout").css("margin-top",$(".home-bg").height()+200);
});

Also, be aware, that when you use background like this, you are fixing an image and whole content will never go below the image itself. 另外,请注意,当您使用这样的背景时,您正在固定图像,并且整个内容永远不会低于图像本身。 See CSS code where min-height: 100%; 请参见CSS代码,其中最小高度:100%;

When you set something as position: fixed it takes it out of the flow of the document. 当您将某物设置为position: fixed它会将其从文档流中移出。 So other elements will not care where it is on the page and those other elements will not position themselves relative to it. 因此,其他元素将不在乎页面在页面上的位置,并且这些其他元素也不会相对于页面定位自己。 That is why your text just shows up at the top. 这就是为什么您的文本仅显示在顶部的原因。 Because the image is fixed. 因为图像是固定的。

I would remove the CSS setting the image as fixed . 我将删除将图像设置为fixed的CSS。

CSS: CSS:

img.home-bg {
  min-height: 100%;
  min-width: 1024px;
  width: 100%;
  height: auto;
  top: 0;
  left: 0;
  margin-top: 100px;
}

Fiddle: http://jsfiddle.net/qd8Ltufw/8/ 小提琴: http : //jsfiddle.net/qd8Ltufw/8/

If you really want the image fixed and the content to scroll over it you can either position the content some how, maybe setting its margin. 如果您确实要固定图像并在内容上滚动,则可以以某种方式定位内容,也可以设置其边距。 Or you can use javascript to get the images height and dynamically set the contents position. 或者,您可以使用javascript获取图像高度并动态设置内容位置。

Fiddle: http://jsfiddle.net/qd8Ltufw/10/ 小提琴: http : //jsfiddle.net/qd8Ltufw/10/

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

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