上一个按钮导致幻灯片显示消失

时间:2017-12-02 19:35:10

标签: javascript html css slideshow

我拍摄了由用户here编辑的Sinisake的简单w3学校幻灯片,并编辑了样式以删除幻灯片计数器和标题,并编辑了prev和next按钮以占用了一半幻灯片的每一侧,光标变化以指示单击。

唯一的问题是当您在第一张幻灯片上单击“上一个”时,previousElementSibling正在调用一个不存在的元素,整个幻灯片显示消失。我知道这种情况正在发生,因为我改变了上一个和下一个位置,但我不知道为什么。

另外,我用display:none隐藏了这些点,但如果我从html和javascript中删除它们,幻灯片就会中断。

我对CSS和html有点了解,但我对Javascript很新,所以我感谢大家的耐心和帮助。

我的代码如下,该页面也在此处托管:http://dominicfortunato.com/Template%20Studio%20Site/index-updated-slideshow-2.html

(function() {

  init(); //on page load - show first slide, hidethe rest

  function init() {

    parents = document.getElementsByClassName('slideshow-container');

    for (j = 0; j < parents.length; j++) {
      var slides = parents[j].getElementsByClassName("mySlides");
      var dots = parents[j].getElementsByClassName("dot");
      slides[0].classList.add('active-slide');
      dots[0].classList.add('active');
    }
  }

  dots = document.getElementsByClassName('dot'); //dots functionality

  for (i = 0; i < dots.length; i++) {

    dots[i].onclick = function() {

      slides = this.parentNode.parentNode.getElementsByClassName("mySlides");

      for (j = 0; j < this.parentNode.children.length; j++) {
        this.parentNode.children[j].classList.remove('active');
        slides[j].classList.remove('active-slide');
        if (this.parentNode.children[j] == this) {
          index = j;
        }
      }
      this.classList.add('active');
      slides[index].classList.add('active-slide');

    }
  }
//prev/next functionality
  links = document.querySelectorAll('.slideshow-container a');

  for (i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      current = this.parentNode;

      var slides = current.getElementsByClassName("mySlides");
      var dots = current.getElementsByClassName("dot");
      curr_slide = current.getElementsByClassName('active-slide')[0];
      curr_dot = current.getElementsByClassName('active')[0];
      curr_slide.classList.remove('active-slide');
      curr_dot.classList.remove('active');
      if (this.className == 'next') {

        if (curr_slide.nextElementSibling.classList.contains('mySlides')) {
          curr_slide.nextElementSibling.classList.add('active-slide');
          curr_dot.nextElementSibling.classList.add('active');
        } else {
          slides[0].classList.add('active-slide');
          dots[0].classList.add('active');
        }

      }

      if (this.className == 'prev') {

 	 	if (curr_slide.previousElementSibling.classList.contains('mySli‌​des')) {
          curr_slide.previousElementSibling.classList.add('active-slide');
          curr_dot.previousElementSibling.classList.add('active');
        } else {
          slides[slides.length - 1].classList.add('active-slide');
          dots[slides.length - 1].classList.add('active');
        }

      }

    }

  }
})();
 /* Slideshow container */
 
 
.slideshow-container {
  max-width: 80%;
  position: relative;
  padding-top: 3%;
  padding-bottom: 2%;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: url('Images/arrow.png'), w-resize;
  float: left;
  position: absolute;
  width: 50%;
  height: 90%;
  margin-right: auto;
  margin-left: auto;
  transition: 0.3s ease;
  border-radius: 0 0px 0px 0;
}

/* Position the "next button" to the right */
.next {
  cursor: url('Images/arrow.png'), e-resize;
  right: 0;
  border-radius: 0px 0 0 0px;

}

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

.mySlides {
  display:none;
}
.active-slide {
  display:block;
}

.active-slide-1 {
  display:block;
}

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

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


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

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

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

.text-container {
	z-index: 300;
	max-width: 80%;
	float: center;
	text-align: left;
	position: relative;
	margin-right: auto;
	margin-left: auto;
	padding-bottom: 2%;
	font-family:"GT Walsheim", Calibri, sans-serif;
}

.project-description {
	width: 100%;
	font-size: 36px;
	margin-left: 20px;
	font-family:"GT Walsheim", Calibri, sans-serif;
	display: inline;
}

.project-title {
	width: 100%;
	display: inline;
	margin-left: 150px;
	text-decoration: underline;
	font-size: 36px; 
}
<div class="slideshow-container">
  <a class="prev" ></a>
  <a class="next"></a>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://placehold.it/1000x350" style="width:100%">
  </div>

<div style="text-align:center">
  <span class="dot"></span>
  <span class="dot"></span>
  <span class="dot"></span>
  <span class="dot"></span>
</div>  
</div>
<div class="text-container">
	<div class="project-title">Rust Belt Riders</div>
	<div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
  </div>

<br>

<div class="slideshow-container">
  <a class="prev" ></a>
  <a class="next"></a>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://placehold.it/1000x350" style="width:100%">
  </div>

<div style="text-align:center">
  <span class="dot"></span>
  <span class="dot"></span>
  <span class="dot"></span>
</div>  
</div>
<div class="text-container">
	<div class="project-title">Rust Belt Riders</div>
	<div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
  </div>

<br>

1 个答案:

答案 0 :(得分:0)

这对我有用:(你可能忘记了第二个滑块中的一个点)。看看这个小提琴:https://jsfiddle.net/prtuxoxx/

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;

/**
 *
 * @author Boggs1
 */
public class Caesar 
{

private static Scanner sc;
private static FileWriter fw;
private static BufferedWriter bw;

public static char decodeChar(int n, char ch)
{
    int ord;
    if (ch >= 'A' && ch <= 'Z')
    {
        ord = ch - 'A';
        ord += n;
        ord %= 26;
        return (char)(ord + 'A');
    }
    else if (ch >= 'a' && ch <= 'z')
    {
        ord = ch - 'a';
        ord += n;
        ord %= 26;
        return (char)(ord + 'a');
    }
    else
    {
        return ch;
    }
}

public static void main(String[] args)
{
    System.out.println("Number of command-line arguments: " + args.length);
    File inputFile = new File("decode.txt");
    File outputFile = new File("decoded.txt");
    try 
    {
        sc = new Scanner(inputFile);
        int shift = sc.nextInt();
        String decoded = "";
        while( sc.hasNextLine() )
        {
            String line = sc.nextLine();
            for(int i=0; i<line.length(); i++)
            {
                decoded += decodeChar(shift, line.charAt(i));                    
            }
            decoded += '\n';
        }
        System.out.println(decoded);
        try
        {
            fw = new FileWriter(outputFile);
            bw = new BufferedWriter(fw);
            bw.write(decoded);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
}

}

和JS:

<div class="slideshow-container">
        <a class="prev" ></a>
        <a class="next"></a>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://placehold.it/1000x350" style="width:100%">
        </div>

      <div style="text-align:center">
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
      </div>  
    </div>
    <div class="text-container">
        <div class="project-title">Rust Belt Riders</div>
        <div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
    </div>

      <br>

    <div class="slideshow-container">
        <a class="prev" ></a>
        <a class="next"></a>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://placehold.it/1000x350" style="width:100%">
        </div>

      <div style="text-align:center">
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
        <span class="dot"></span>
      </div>  
    </div>
    <div class="text-container">
        <div class="project-title">Rust Belt Riders</div>
        <div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
    </div>