简体   繁体   English

褪色背景图像和顶部图像

[英]Fading background image with image ontop

I am trying to create a div that has a fading background image with another fixed image ontop of this. 我正在尝试创建一个div,该div具有褪色的背景图像,并在其上面有另一个固定图像。 The svg image I am trying to add ontop of the doesn't stay fixed with the div, It also fades in and out with the background. 我试图在svg上添加的svg图像不会随着div保持固定,它也会随着背景淡入和淡出。 How can i stop the img fading and keep centered in the div? 如何停止img淡入淡出并保持div居中? I have tried using absolute position but when the page is in mobile view the image moves offscreen, 我尝试使用绝对位置,但是当页面在移动视图中时,图像会移出屏幕,

This is what i have currently. 这就是我目前所拥有的。

<div class="slideme" id="slideme"> 
  <img style="max-width: 90%; "src="img/header-logo.svg" class="img-responsive">                    
</div> 

CSS: CSS:

#slideme{
  position:relative;
  width:100%;
  height:200px;
}

#slideme div{
  position:absolute;
  width:100%;
  height:200px;
}

The problem is that when you fade out the div, you fade out also the contents of that div. 问题是,当您淡出div时,您也会淡出该div的内容。 Basically, all goes away, including your second svg image which is contained by the #slidme div which also have the other image as background. 基本上,一切都会消失,包括#slidme div中包含的第二个svg图像,该图像也将另一个图像作为背景。

First, you should probably refactor your code to look more like this: 首先,您可能应该将代码重构为如下所示:

<div class="main-slider-container">
    <img style="max-width: 90%; "src="img/header-logo.svg" class="img-responsive">
    <div class="slideme" id="slideme"></div>    
</div>

CSS 的CSS

.main-slider-container{
  position:relative;
}

#slideme div{
  position:absolute;
  width:100%;
  height:200px;
  top:0px;
  right:0px;
  z-index:0;
}

> img {
  position:absolute;
  width:100%;
  height:200px;
  top:0px;
  left:0px;
  z-index:1;
}

With this structure, you can individually, hide the #slideme div without affecting the svg image 使用此结构,您可以单独隐藏#slideme div而不影响svg图像

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

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