CSS-带有图像渐变器的绝对位置和相对位置

时间:2019-04-22 15:18:56

标签: javascript css image css-position

我正在使用图像推子程序,但是我不了解绝对定位。如果屏幕调整大小,我的图像会很好地褪色并调整我想要的方式。但是我有两个问题。 Div#2被图像遮盖了。我希望div2始终显示在图像div下方。另外,我在图像上有控制按钮。我想要他们在中间。我认为使用top:50%可以做到这一点,但事实并非如此。这是一个例子...

var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,5000);

function nextSlide(){goToSlide(currentSlide+1);}

function previousSlide(){goToSlide(currentSlide-1);}

function goToSlide(n){
    slides[currentSlide].className = 'slide';
    currentSlide = (n+slides.length)%slides.length;
    slides[currentSlide].className = 'slide showing';}

var next = document.getElementById('next');
var previous = document.getElementById('previous');

next.onclick = function(){nextSlide();};
previous.onclick = function(){previousSlide();};
#slides {position: relative}

.slide{ 
position: absolute;
left: 0px;
top: 0px;
width:100%;
height:auto;
min-height:300px;
object-fit:cover;
opacity: 0; 
box-sizing:border-box;
transition: opacity 2s;}


.showing{opacity: 1;}

.controls{
background: transparent;
color: #fff;
font-size: 30px;
cursor: pointer;
border: 1px solid #555;
width: 30px;
position: absolute;
}

.controls:hover{ opacity:.5}

.fadenext{right: 10px; top: 50%;}
.fadeprev{left:  10px; top: 50%;}
<br><br>
<div id="slides">
   <img src='https://www.panotools.org/dersch/Monp.JPG' class="slide showing">
   <img src='https://www.panotools.org/dersch/StBp.JPG'  class="slide">
 
   <button class="controls fadeprev" id="previous">&lt;</button>
   <button class="controls fadenext" id="next">&gt;</button>
</div>


<div style='margin-top:40px;border:1px solid red;width:200px;height:100px'>
This is Div # 2</div>

2 个答案:

答案 0 :(得分:0)

我已经修改了您的代码段以解决您的问题。

  • 添加margin-top而不是top将解决您的问题, 控件。
  • Div 2现在将始终保持在滑块下方。

P.S。我将您的div2内联样式更改为整齐。

var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide, 5000);

function nextSlide() {
  goToSlide(currentSlide + 1);
}

function previousSlide() {
  goToSlide(currentSlide - 1);
}

function goToSlide(n) {
  slides[currentSlide].className = 'slide';
  currentSlide = (n + slides.length) % slides.length;
  slides[currentSlide].className = 'slide showing';
}

var next = document.getElementById('next');
var previous = document.getElementById('previous');

next.onclick = function() {
  nextSlide();
};
previous.onclick = function() {
  previousSlide();
};
* {
  margin: 0;
  padding: 0;
}

#slides {
  position: relative
}

.slide {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 100%;
  height: auto;
  min-height: 300px;
  object-fit: cover;
  opacity: 0;
  box-sizing: border-box;
  transition: opacity 2s;
}

.showing {
  opacity: 1;
}

.controls {
  background: transparent;
  color: #fff;
  font-size: 30px;
  cursor: pointer;
  border: 1px solid #555;
  width: 30px;
  position: absolute;
}

.controls:hover {
  opacity: .5
}

.fadenext {
  right: 10px;
  margin-top: 25%;
}

.fadeprev {
  left: 10px;
  margin-top: 25%;
}

.div2 {
  margin-top: 50%;
  border: 1px solid red;
  width: 200px;
  height: 100px;
}
<br><br>
<div id="slides">
  <img src='https://www.panotools.org/dersch/Monp.JPG' class="slide showing">
  <img src='https://www.panotools.org/dersch/StBp.JPG' class="slide">

  <button class="controls fadeprev" id="previous">&lt;</button>
  <button class="controls fadenext" id="next">&gt;</button>
</div>


<div class="div2">This is Div # 2</div>

答案 1 :(得分:0)

当您使用“顶部”样式时,使用基于百分比的排名是不可行的。因此,要实现您想要的目标,请改用margin-top。如下图所示:

.fadenext{right: 10px; margin-top: 25%;}
.fadeprev{left: 10px; margin-top: 25%;}

对于您的div2,只需将其样式更改为:

margin-top: 50%