Scroll to top
© 2022, Empty Code | All Rights Reserved

Autoplay Text Slider by using HTML, CSS & JavaScript


Rahul Kumar Sharma - August 13, 2020 - 0 comments

In the dynamic world of web development, incorporating interactive elements into your website can significantly enhance user engagement. One such compelling feature is the Autoplay Text Slider. This blog post will walk you through the step-by-step process of creating a captivating Autoplay Text Slider using HTML, CSS, and JavaScript. By the end of this tutorial, you’ll have an attention-grabbing text slider that seamlessly transitions through different messages, effectively capturing your visitors’ attention.

Prerequisites:

Before we begin, make sure you have a basic understanding of HTML, CSS, and JavaScript. Familiarity with HTML elements, CSS styling, and basic JavaScript concepts will be helpful for following along.

Step 1: Setting Up the HTML Structure:

First, let’s set up the basic HTML structure for our text slider. We’ll create a container to hold the slider content and individual slides for each piece of text.

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

Step 2: Styling the Slider:

Next, let’s add some CSS styles to create a visually appealing slider. We’ll define the dimensions, add borders, and style the individual slides.

/* 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;}

Step 3: Implementing the JavaScript Logic:

Now comes the magic part! Let’s add JavaScript to enable the Autoplay functionality for our Text Slider. The script will automatically switch between slides at regular intervals, creating a captivating carousel effect.

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

window.onload= function () {
  setInterval(function(){ 
     plusSlides(1);
  }, 3000);
 }

Conclusion:

In conclusion, this auto play text slider tutorial demonstrates how to build a dynamic and interactive slider using HTML, CSS, and JS. By combining the power of these technologies, you can create a visually appealing slider that engages your website visitors. Implementing the slider HTML CSS JS code provided in this guide allows you to showcase your content in a captivating and user-friendly manner. Embrace the versatility of the slider and unleash your creativity to enhance the overall user experience. With this newfound knowledge, you are well-equipped to integrate a sophisticated text slider into your web projects, captivating your audience with smooth transitions and eye-catching animations. Happy coding!


Example

Check out this example of an Auto-Play Text Slider on CodePen! With just a few lines of HTML, CSS, and JavaScript, you can showcase your text in a visually appealing manner. Impress your visitors with seamless transitions and auto-play functionality. Don’t miss this amazing Text Slider on CodePen – try it now and elevate your website!

See the Pen Autoplay Text Slider by Empty Code (@emptycodeIN) on CodePen.

Related posts

Post a Comment

Your email address will not be published. Required fields are marked *