简体   繁体   中英

How TO - Quotes Slideshow

Wondering If someone could help me, I'm relatively new to Javascript. I'm trying to use the QUOTES Slideshow code using the link below, however, i can't seem to solve the issue and Javascript shows the following errors:

https://www.w3schools.com/howto/howto_js_quotes_slideshow.asp

  • ERROR 'plusSlides' is defined but never used. {no-unused-vars}
  • ERROR 'currentSlides' is defined but never used. {no-unused-vars}

For some reason, when you copy and paste all the code to a html doc it seems to work but when I try to separate the code into their respective locations It doesn't seem to work showing the errors above.

Code shown below, I would greatly appreciate it if someone could assist me / or even point me in the right direction to better code. Ideally I wouldn't mind having 3 div tags (Inc 3 quotes) per slideshow because this code only give me one quote per slideshow.

 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"; }
 @charset "utf-8"; /* CSS Document */ * {box-sizing: border-box} body {font-family: Verdana, sans-serif; margin:0} /* Slideshow container */.slideshow-container { position: relative; background: #f1f1f1f1; } /* Slides */.mySlides { display: none; padding: 80px; text-align: center; } /* Next & previous buttons */.prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -30px; padding: 16px; color: #888; font-weight: bold; font-size: 20px; border-radius: 0 3px 3px 0; user-select: none; } /* Position the "next button" to the right */.next { position: absolute; 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); color: white; } /* The dot/bullet/indicator container */.dot-container { text-align: center; padding: 20px; background: #ddd; } /* The dots/bullets/indicators */.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; } /* Add a background color to the active dot/circle */.active, .dot:hover { background-color: #717171; } /* Add an italic font style to all quotes */ q {font-style: italic;} /* Add a blue color to the author */.author {color: cornflowerblue;}
 <,doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width. initial-scale=1"> <link href="Untitled-2.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="untitled-3.js"></script> </head> <body> <div class="slideshow-container"> <div class="mySlides"> <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q> <p class="author">- John Keats</p> </div> <div class="mySlides"> <q>But man is not made for defeat. A man can be destroyed but not defeated.</q> <p class="author">- Ernest Hemingway</p> </div> <div class="mySlides"> <q>I have not failed, I've just found 10.000 ways that won't work.</q> <p class="author">- Thomas A. Edison</p> </div> <a class="prev" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> <div class="dot-container"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div> </body> </html>

You can make these changes to obtain the right results You have not specify width and height to slideshow-container,also I see a slidesShow function not working properly until full Dom loads,so I applied a load() to body that's why code is not working properly HTML&css

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style type="text/css" media="all">
    /

    * {
      margin: 0;
      padding: 0;
    }
    body {
      font-family: Verdana, sans-serif;
      margin: 0
    }

    /* Slideshow container */
    .slideshow-container {
      position: relative;
      width: 100%;
      height: 300px;
      background: #f1f1f1f1;
    }

    /* Slides */
    .mySlides {
      display: none;
      
      padding: 80px;
      text-align: center;
    }

    /* Next & previous buttons */
    .prev, .next {
      cursor: pointer;
      position: absolute;
      top: 50%;
      width: auto;
      margin-top: -30px;
      padding: 16px;
      color: #888;
      font-weight: bold;
      font-size: 20px;
      border-radius: 0 3px 3px 0;
      user-select: none;
    }

    /* Position the "next button" to the right */
    .next {
      position: absolute;
      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);
      color: white;
    }

    /* The dot/bullet/indicator container */
    .dot-container {
      text-align: center;
      padding: 20px;
      background: #ddd;
    }

    /* The dots/bullets/indicators */
    .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;
    }

    /* Add a background color to the active dot/circle */
    .active, .dot:hover {
      background-color: #717171;
    }

    /* Add an italic font style to all quotes */
    q {
      font-style: italic;
    }

    /* Add a blue color to the author */
    .author {
      color: cornflowerblue;
    }
  </style>
  <script type="text/javascript" src="Prb4.js"></script>
</head>
<body onload="Load()">

  <div class="slideshow-container">

    <div class="mySlides">
      <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
      <p class="author">
        - John Keats
      </p>
    </div>

    <div class="mySlides">
      <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
      <p class="author">
        - Ernest Hemingway
      </p>
    </div>

    <div class="mySlides">
      <q>I have not failed. I've just found 10,000 ways that won't work.</q>
      <p class="author">
        - Thomas A. Edison
      </p>
    </div>

    <a class="prev" onclick="plusSlides(-1)">❮</a>
    <a class="next" onclick="plusSlides(1)">❯</a>

  </div>

  <div class="dot-container">
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
    <span class="dot" onclick="currentSlide(3)"></span>
  </div>
</body>
</html>

Js

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

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

function showSlides(n) {
  var i;
  let 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", "");
  }
  console.log(slides[slideIndex-1])
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM