简体   繁体   English

CSS - 图像推子的绝对和相对位置

[英]CSS - Absolute and Relative Position with image fader

I'm working with an image fader program, but I'm not understanding absolute positioning. 我正在使用图像推子程序,但我不理解绝对定位。 I have the images fading nicely and resizing the way I want if the screen resizes. 如果屏幕调整大小,我可以很好地淡化图像并调整我想要的方式。 but I have 2 problems. 但我有两个问题。 Div#2 gets covered up by the images. Div#2被图像覆盖。 I want div2 to always appear below the image div. 我希望div2始终出现在图像div下方。 Also, I have control buttons on the images. 另外,我在图像上有控制按钮。 I want them in the middle. 我希望他们在中间。 I thought using top:50% would do that, but it's not. 我认为使用top:50%会这样做,但事实并非如此。 Here's an example... 这是一个例子......

 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> 

I've amended your snippet to fix your issues. 我修改了你的代码片段以解决你的问题。

  • Adding margin-top instead of top will fix your issue with the controls. 添加margin-top而不是top将解决您的控件问题。
  • Div 2 will now always remain below your slider. Div 2现在将始终保持在滑块下方。

PS I moved your div2 inline styles to keep it neat. PS我移动你的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> 

It's not feasible to use % based positions when you use "top" style. 使用“顶级”样式时,使用基于%的位置是不可行的。 So to achieve what you want to do, use margin-top instead. 因此,要实现您想要的功能,请使用margin-top。 As shown below: 如下所示:

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

And for your div2, just change it's style to: 对于你的div2,只需改变它的风格:

margin-top: 50%

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

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