当宽度未知时,如何为Javascript幻灯片放映图像?

时间:2018-01-25 20:11:32

标签: javascript html css

我在幻灯片中播放的目录中有一个图像列表 这些图像是随机大小的。无论宽度如何,我都需要将它们放在中心位置 由于宽度不同,我无法设置手动保证金。任何帮助将不胜感激。



var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
}
&#13;
.imgClass {
  width: auto;
  height: 380px;
}

.slideContainersContainer {
  width: auto;
  height: 100%;
  float: left;
}

.slideshow-container {
  width: auto;
  background-color: #ffffff;
  transition: ease-in-out .7s;
  flexbox: flex;
  margin-left: auto;
  margin-right: auto;
}

.mySlides {
  display: none;
}

.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  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);
}

.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

.dot {
  cursor: pointer;
  height: 5px;
  width: 5px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
  padding: .2%;
}

.active,
.dot:hover {
  background-color: #717171;
}

.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: 0
  }
  to {
    opacity: 1
  }
}
&#13;
<div class="slideContainersContainer">
  <div class="slideshow-container">

    <!-- This section calls the first image. -->

    <div class="mySlides fade">
      <div class="numbertext">1 / 1</div>
      <img class="imgClass" alt="" src="images/paintings/paintings1.png">
      <div class="text">
        <span class="title">Caption Text</span>
        <span class="origStat">Originoal Status:Sold</span>
        <a class="buyPrints">Purchase Prints</a></div>
    </div>

    <!-- This section calls the second image. -->

    <div class="mySlides fade">
      <div class="numbertext">1 / 2</div>
      <img class="imgClass" alt="" src="images/paintings/paintings2.png">
      <div class="text">
        <span class="title">Caption Text</span>
        <span class="origStat">Originoal Status:Sold</span>
        <a class="buyPrints">Purchase Prints</a></div>
    </div>
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
  </div><br>
  <div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  </div></div>
&#13;
&#13;
&#13;

我添加了剩下的代码,对此造成的不便表示歉意。任何帮助都会受到极大的赞赏。

1 个答案:

答案 0 :(得分:1)

您可以将flexbox与图像的父容器一起使用。

.mySlides {
    display: flex;
    justify-content: center;
    align-items: center;
  }