Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Alexandra Cianciara
Alexandra Cianciara
2,465 Points

Slides, rotating testimonials and hero banners. JavaScript

Hello, I wonder if anyone can help me. I am a beginner in HTML, CSS and JavaScript.

I added slides to my website and they are ok but they don't appear when I enter the website, only wen I press the arrows. I'd like to be able to see when upon entering the website. Here is my code

<div class="slideshow-container">

              <div class="mySlides  fade" id="hero">
                        <div class="slide text">         
                            <h2>LOCALLY FOR MORE THAN 10 YEARS</h2>
                            <h3 class="container-cta">FIND OUT MORE</h3>  
                        </div>  
              </div>   

              <div class="mySlides fade" id="hero">
                        <div class="slide text">         
                            <h2>FOR ALL GOALS AND ABILITIES</h2>
                            <h3 class="container-cta">LEARN MORE</h3>  
                       </div>  
              </div>   

              <div class="mySlides fade" id="hero">
                        <div class="slide text">         
                            <h2>NEW MEMBERS WELCOME</h2>
                            <h3 class="container-cta">TRY FOR FREE</h3>  
                       </div>  
              </div>

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

              <!-- The dots/circles -->

              <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> 

              </div> 

And also for testimonials

 <div class="slideshow-container">

              <!-- Full-width slides/quotes -->
              <div class="mySlides">
                <q>After few months of joining, I already see how much fitter I became.</q>
                <p class="author">- Alex</p>
              </div>

              <div class="mySlides">
                <q>My running really improved since I started the boot camp a year ago and now I'm training for the marathon.</q>
                <p class="author">- Mark</p>
              </div>

              <div class="mySlides">
                <q>My running really improved since I started the boot camp a year ago and now I'm training for the marathon.</q>
                <p class="author">- Mark</p>
              </div>

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

            <!-- Dots/bullets/indicators -->
            <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>   

And this is CSS

.slideshow-container {
  position: relative;
  background: #f1f1f1f1;      
  width: 100%;    
}

/* Slides */
.mySlides {
  display: none;
  padding: 40px;
  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;}


/*hero rotating*/

#hero {
background-image: url(img/robreedbootcamp_hero.jpg);
background-size: cover;
background-repeat: no-repeat;    
background-position: center;
width: 1030px;   
height: 230px;
margin-bottom: 20px;
color: white;        
}

And this is JavaScript

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

1 Answer

The first thing I think you should try is to move the first showSlides(slideIndex); down to the bottom, after you've assigned/declared the showSlides function.

You don't get an error because of something in JavaScript called hoisting, so the function will currently just return undefined when called where it is now.

And one other thing that isn't quite right is that you have 3 tags with the id attribute of hero. IDs should be unique, so it's invalid HTML. Try giving them numbered IDs, and then adjusting the CSS to either use those numbered IDs or just the class.