【问题标题】:I have some issues while making an slideshow我在制作幻灯片时遇到了一些问题
【发布时间】:2019-12-04 19:13:05
【问题描述】:

我正在尝试制作幻灯片,但是当它移动到下一张(或上一张)图像时,背景上没有任何内容。

我希望你将看到的图像出现在你所看到的图像后面,就像你洗牌时一样。

此外,当您单击右箭头或左箭头时,我希望有不同的动画,以匹配它的运动。

这是我现在使用的代码:

Code

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">
<style>
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  overflow: hidden;
  max-width: 300px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

</style>
</head>
<body>

<div class="slideshow-container">

 <div class="mySlides animated slideInLeft">
  <img src="https://pruebas20.webcindario.com/img_snow_wide.jpg" style="width:100%">
 </div>

 <div class="mySlides animated slideInRight">
  <img src="https://pruebas20.webcindario.com/img_nature_wide.jpg" style="width:100%">
 </div>

 <div class="mySlides slideInRight">
  <img src="https://pruebas20.webcindario.com/img_mountains_wide.jpg" style="width:100%">
 </div>

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

</div>

<script>
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";
}
</script>

</body>
</html> 

谢谢!

【问题讨论】:

    标签: html css slide


    【解决方案1】:

    问题是除了当前幻灯片之外,您使用 javascript 和 css 都隐藏了所有内容。

    for (i = 0; i < slides.length; i++) {
          slides[i].style.display = "none";  
      }
    .mySlides {display: none}

    您需要删除它,但改为更改幻灯片的 z-index 以使当前幻灯片位于顶部,同时添加 slideInRight/slideInLeft 类。您还需要绝对定位容器内的幻灯片,以便它们占据相同的位置。

    【讨论】:

      【解决方案2】:

      是的,马克·费舍尔是正确的。您需要使用幻灯片的 z-index。老实说,如果你想有很酷的过渡等,使用诸如Slick Slider之类的插件可能会更容易。 我根据您的代码创建了一个 codepen。它并不完美,但也许您可以了解如何做到这一点。您的幻灯片需要绝对定位在幻灯片包装内,以便在您在幻灯片之间移动时彼此“显示”。我在位置上使用了 CSS 过渡来为幻灯片设置动画。

      .mySlides {position: absolute; left: 0; z-index: 0;transition: left 1s linear; width: 300px; height: 105px;}
      

      https://codepen.io/moorehannah/pen/eqdXby

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        相关资源
        最近更新 更多