Z-index“中断”幻灯片单击按钮

时间:2018-06-27 11:26:45

标签: javascript html css slideshow

我遇到了这个问题,滚动时导航栏在幻灯片后面。我有解决此问题的想法:在css文件中添加“ z-index:-10;”行。

它确实有效,导航栏与幻灯片重叠,但是按钮不再起作用。这意味着我无法更改图像,因为幻灯片显示不是自动进行的。

幻灯片的HTML部分:

<div class="slideshow-container">


    <div class="mySlides fade">
      <div class="numbertext">1 / 7</div>
      <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
      <div class="text1">text</div>
    </div>

    <div class="mySlides fade">
      <div class="numbertext">2 / 7</div>
      <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
      <div class="text1">text</div>
    </div>

    <div class="mySlides fade">
      <div class="numbertext">3 / 7</div>
      <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
      <div class="text1">text</div>
    </div>

    <div class="mySlides fade">
        <div class="numbertext">4 / 7</div>
        <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
        <div class="text1">text</div>
      </div>

      <div class="mySlides fade">
        <div class="numbertext">5 / 7</div>
        <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
        <div class="text1">text</div>
      </div>

      <div class="mySlides fade">
        <div class="numbertext">6 / 7</div>
        <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
        <div class="text1">text</div>
      </div>

      <div class="mySlides fade">
        <div class="numbertext">7 / 7</div>
        <img src="Img.jpg" width="825px" height="550px" class="slidehowfoto">
        <div class="text1">text</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> 
    <span class="dot" onclick="currentSlide(3)"></span> 
    <span class="dot" onclick="currentSlide(4)"></span>
    <span class="dot" onclick="currentSlide(5)"></span>
    <span class="dot" onclick="currentSlide(6)"></span>
    <span class="dot" onclick="currentSlide(7)"></span>
  </div>
  <script src="slideshow.js"></script>

幻灯片的CSS部分:

.slideshow-container {
  width: 825px;
  position: relative;
  margin: auto;
  height: 550px;
  z-index: -10;
}


.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);
}


.text1 {
  color:white;
  font-size: 20px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
  font-weight: bold;
  background: #F5A105
}


.numbertext {
  color:#F5A105;
  font-size: 17px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
  font-weight: bold;
}


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

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

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

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

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

.slidehowfoto {

    border-radius: 15px;
}

JavaScript部分:

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
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";
}

导航栏代码:

  <ul>
            <li><img src="Profielfoto.PNG" class="header"></li>
            <li><a href="Index.html">HOME</a></li>
            <li><a href="Menukaart.html">MENUKAART</a></li>  
            <li><a href="Eigenkeuken.html">UIT EIGEN KEUKEN</a></li>
            <li><a href="Feestjes.html">FEESTJES</a></li>
            <li><a href="#contact">CONTACT</a></li>
            <li><img src="Profielfoto.PNG" class="header"></li>

CSS导航栏:

ul {
    list-style-type: none;
    margin: 0;
    padding: 30px 25px;
    overflow: hidden;
    background-color: #F7B233;
    position: -webkit-sticky; 
    position: sticky;
    top: 0;
    border-radius: 15px;
    font: arial;
}

li {
    float: left;
    text-decoration-color: black;
}


li a {
    display: block;
   background-color:#F7B233;
    color: white;
    text-align: center;
    padding: 25px 15px;
    text-decoration: burlywood;
    font-weight: bold;
    opacity: 1;
    transition: 0.3s;
    outline: none;
    border-radius: 15px;
    box-shadow: 0 7px white;

}

li a:hover {opacity: 0.6}
a{

    border-right: 5px white;
    border-radius: 5px;

}


.header { 

    position:left;
    width: 150px;
    height: 70px;


} 

1 个答案:

答案 0 :(得分:0)

您无需在此处添加z-index。将您的导航栏代码保留在<nav></nav>中,一切都会如您所愿。下面是工作代码:

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
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";
}
.slideshow-container {
  width: 825px;
  position: relative;
  margin: auto;
  height: 550px;
  margin-top: 20px;
}

.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);
}

.text1 {
  color: white;
  font-size: 20px;
  padding: 8px 12px;
  position: absolute;
  bottom: 12px;
  width: calc(100% - 24px);
  text-align: center;
  font-weight: bold;
  background: #F5A105
}

.numbertext {
  color: #F5A105;
  font-size: 17px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
  font-weight: bold;
}

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

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

/* Fading animation */

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

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

@keyframes fade {
  from {
    opacity: .4
  }
  to {
    opacity: 1
  }
}

.slidehowfoto {
  border-radius: 15px;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 30px 25px;
  overflow: hidden;
  background-color: #F7B233;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  border-radius: 15px;
  font: arial;
}

li {
  float: left;
  text-decoration-color: black;
}

li a {
  display: block;
  background-color: #F7B233;
  color: white;
  text-align: center;
  padding: 25px 15px;
  text-decoration: burlywood;
  font-weight: bold;
  opacity: 1;
  transition: 0.3s;
  outline: none;
  border-radius: 15px;
  box-shadow: 0 7px white;
}

li a:hover {
  opacity: 0.6
}

a {
  border-right: 5px white;
  border-radius: 5px;
}

.header {
  position: left;
  width: 150px;
  height: 70px;
}
<nav>
  <ul>
    <li><img src="https://picsum.photos/200/300?image=0" class="header"></li>
    <li><a href="#">HOME</a></li>
    <li><a href="#">MENUKAART</a></li>
    <li><a href="#">UIT EIGEN KEUKEN</a></li>
    <li><a href="#">FEESTJES</a></li>
    <li><a href="#">CONTACT</a></li>
    <li><img src="https://picsum.photos/200/300?image=12" class="header"></li>
  </ul>
</nav>

<div class="slideshow-container">

  <div class="mySlides fade">
    <div class="numbertext">1 / 7</div>
    <img src="https://picsum.photos/200/300?image=3" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 7</div>
    <img src="https://picsum.photos/200/300?image=7" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 7</div>
    <img src="https://picsum.photos/200/300?image=9" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">4 / 7</div>
    <img src="https://picsum.photos/200/300?image=4" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">5 / 7</div>
    <img src="https://picsum.photos/200/300?image=11" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">6 / 7</div>
    <img src="https://picsum.photos/200/300?image=1" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">7 / 7</div>
    <img src="https://picsum.photos/200/300?image=5" width="825px" height="550px" class="slidehowfoto">
    <div class="text1">text</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>
  <span class="dot" onclick="currentSlide(3)"></span>
  <span class="dot" onclick="currentSlide(4)"></span>
  <span class="dot" onclick="currentSlide(5)"></span>
  <span class="dot" onclick="currentSlide(6)"></span>
  <span class="dot" onclick="currentSlide(7)"></span>
</div>

这是JSFiddle:http://jsfiddle.net/epots2bu/15/