中心绝对定位<a> to absolutely positioned image

时间:2017-07-06 16:28:48

标签: html css

I try to make text that will be centered in the middle of the image, but the problem is that in my attempts I did not succeed to make it with resizing website. Any suggestions how to fix it and what I do wrong?

* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}

.imageSlide-container {
    position: relative;
    margin: auto;
}
.imageSlide {
    position: absolute;
    width: 100%;
    height: auto;
    opacity: 0;
    z-index: 1;

    -webkit-transition: opacity 2.5s;
    -moz-transition: opacity 2.5s;
    -o-transition: opacity 2.5s;
    transition: opacity 2.5s;
}
.showing {
    opacity: 1;
    z-index: 2;
}
            
.prev, .next {
    z-index: 3;
    position: absolute;
    top: 50%;
    width: auto;
    cursor: pointer;
    color: red;
    /*margin-top: -22px;*/
    padding: 15px;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;

}
.next {
    right: 0;
    border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
    background-color: rgba(0,0,0,0.8)
}
@media only screen and (max-width: 300px) {
    .prev, .next,.text {font-size: 11px}
<div class="imageSlide-container">
    <img class="imageSlide showing" src="https://www.w3schools.com/howto/img_nature_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_fjords_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_mountains_wide.jpg">

    <a class="prev" onclick="plusSlide-button(-1)">&#10094;</a>
    <a class="next" onclick="plusSlide-button(1)">&#10095;</a>
</div>

2 个答案:

答案 0 :(得分:1)

这是使用下面评论中的代码的更新版本,我将top:0添加到.imageSlide并解决了滑块丢弃上一个图像问题。

https://jsfiddle.net/k391zyn1/3/

HTML:

<div class="imageSlide-container">
    <img class="imageSlide showing" src="https://www.w3schools.com/howto/img_nature_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_fjords_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_mountains_wide.jpg">

    <a class="prev" onclick="plusSlide-button(-1)">&#10094;</a>
    <a class="next" onclick="plusSlide-button(1)">&#10095;</a>
</div>

的CSS:

* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}

.imageSlide-container {
    position: relative;
    margin: auto;
}
.imageSlide {
    position: absolute;
    top:0;
    width: 100%;
    height: auto;
    opacity: 0;
    z-index: 1;

     -webkit-transition: opacity 2.5s;
    -moz-transition: opacity 2.5s;
    -o-t ransition: opacity 2.5s;
    transition: opacity 2.5s;
}
.showing {
    position: static;
    opacity: 1;
    z-index: 2;
}


.prev, .next {
    z-index: 3;
    position: absolute;
    top: 50%;
    width: auto;
    cursor: pointer;
    color: red;
    margin-top: -22px;
    padding: 15px;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;

}
.next {
    right: 0;
    border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
    background-color: rgba(0,0,0,0.8)
}
@media only screen and (max-width: 300px) {
    .prev, .next,.text {font-size: 11px}
}

的javascript:

  var slides = document.querySelectorAll('.imageSlide');
   var currentSlide = 0;
   var slideInterval = setInterval(nextSlide,3000);

   function nextSlide(){
       slides[currentSlide].className = 'imageSlide';
       currentSlide = (currentSlide+1)%(slides.length);
       console.log('currentSlide',currentSlide);
       slides[currentSlide].className = 'imageSlide showing';
   }

答案 1 :(得分:1)

我通过将position: static添加到.showing解决了这个问题,因为您看到的图片可能未设置为position: absolute,因此按钮中的代码top: 50%可能来自某些内容。

&#13;
&#13;
* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}

.imageSlide-container {
    position: relative;
    margin: auto;
}
.imageSlide {
    position: absolute;
    width: 100%;
    height: auto;
    opacity: 0;
    z-index: 1;

    -webkit-transition: opacity 2.5s;
    -moz-transition: opacity 2.5s;
    -o-transition: opacity 2.5s;
    transition: opacity 2.5s;
}
.showing {
    position: static;
    opacity: 1;
    z-index: 2;
}
            
.prev, .next {
    z-index: 3;
    position: absolute;
    top: 50%;
    width: auto;
    cursor: pointer;
    color: red;
    margin-top: -22px;
    padding: 15px;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;

}
.next {
    right: 0;
    border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
    background-color: rgba(0,0,0,0.8)
}
@media only screen and (max-width: 300px) {
    .prev, .next,.text {font-size: 11px}
&#13;
<div class="imageSlide-container">
    <img class="imageSlide showing" src="https://www.w3schools.com/howto/img_nature_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_fjords_wide.jpg">
    <img class="imageSlide" src="https://www.w3schools.com/howto/img_mountains_wide.jpg">

    <a class="prev" onclick="plusSlide-button(-1)">&#10094;</a>
    <a class="next" onclick="plusSlide-button(1)">&#10095;</a>
</div>
&#13;
&#13;
&#13;