Responsive Image Slider with Text

Responsive Image Slider with Text
Code Snippet: Pure JavaScript Image Slider
Author: Lawrence Weiss
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 50,188
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a responsive image slider with text. It renders a dynamic image carousel with a title, subtitle, background URL and call to action link in each slide’s template.

The image slider comes with arrows buttons to navigate next and previous images with infinite scroll. Besides this, you can also control the carousel behavior with its available configuration options.

The text portion of the carousel allows you to add heading/title, paragraphs/description and a call to action button. Moreover, you can easily customize the working of the plugin by just updating the configuration options.

How to Create Responsive Image Slider with Text

1. First of all, create a div element with a unique id “mySlider” in which the slider will be rendered dynamically. Similarly, create a div element with an id attribute “sliderNav” and place two divs tags inside it with “sliderPrev” and “sliderNext” id. So, the HTML structure for the image slider with text looks like below:

  1. <div id="mySlider"></div>
  2. <div id="sliderNav">
  3. <div id="sliderPrev" onclick="prevSlide();"><i class="fa fa-chevron-left"></i></div>
  4. <div id="sliderNext" onclick="nextSlide();"><i class="fa fa-chevron-right"></i></div>
  5. </div>

2. After that, style the image slider with the following CSS styles. The styles for the body element are optional, you can exclude them. Likewise, you can also set the custom values for CSS attributes in order to customize the image slider.

  1. body {
  2.  
  3. text-align: center;
  4. font-size: 18px;
  5. background-size: cover;
  6. color: #fff;
  7. font-family: sans-serif;
  8. margin: 0;
  9. padding-top: 0;
  10. }
  11.  
  12. h1 {
  13. font-size: 48px;
  14. }
  15.  
  16. h4{
  17. font-size: 24px;
  18. }
  19.  
  20. #mySlider a {
  21. padding: 10px 25px;
  22. background-color: #4CA74C;
  23. color: #fff;
  24. border-radius: 25px;
  25. text-decoration: none;
  26. }
  27.  
  28. #mySlider {
  29. overflow: hidden;
  30. position: relative;
  31. width: 100%;
  32. height: 450px;
  33. }
  34.  
  35. .singleSlide {
  36. background-size: cover;
  37. height: 450px;
  38. box-sizing: border-box;
  39. position: absolute;
  40. left: 100%;
  41. width: 100%;
  42. top: 0px;
  43. }
  44.  
  45. .slideOverlay {
  46. background-color: rgba(0, 0, 0, 0.5);
  47. padding: 50px;
  48. height: 100%;
  49. box-sizing: border-box;
  50. line-height: 50px;
  51. }
  52. #sliderNav {
  53. position: relative;
  54. top: -225px;
  55. z-index: 999;
  56. font-size: 42px;
  57. }
  58.  
  59. #sliderNav:hover { cursor: pointer; }
  60.  
  61. #sliderPrev {
  62. position: relative;
  63. float: left;
  64. left: 50px;
  65. }
  66.  
  67. #sliderNext {
  68. position: relative;
  69. float: right;
  70. right: 50px;
  71. }
  72.  
  73. @-webkit-keyframes slideIn {
  74. 100% { left: 0; }
  75. }
  76.  
  77. @keyframes slideIn {
  78. 100% { left: 0; }
  79. }
  80.  
  81. .slideInRight {
  82. left: -100%;
  83. -webkit-animation: slideIn 1s forwards;
  84. animation: slideIn 1s forwards;
  85. }
  86.  
  87. .slideInLeft {
  88. left: 100%;
  89. -webkit-animation: slideIn 1s forwards;
  90. animation: slideIn 1s forwards;
  91. }
  92.  
  93. @-webkit-keyframes slideOutLeft {
  94. 100% { left: -100%; }
  95. }
  96.  
  97. @keyframes slideOutLeft {
  98. 100% { left: -100%; }
  99. }
  100.  
  101. .slideOutLeft {
  102. -webkit-animation: slideOutLeft 1s forwards;
  103. animation: slideOutLeft 1s forwards;
  104. }
  105.  
  106. @-webkit-keyframes slideOutRight {
  107. 100% { left: 100%; }
  108. }
  109.  
  110. @keyframes slideOutRight {
  111. 100% { left: 100%; }
  112. }
  113.  
  114. .slideOutRight {
  115. -webkit-animation: slideOutRight 1s forwards;
  116. animation: slideOutRight 1s forwards;
  117. }

3. Finally, add the following JavaScript code to functionalize the image slider. In the following code, you just need to create slides using slide object’s template. You can create as many slides as you want.

  1. /*-----------------------------------------------------------------
  2. -----------------------------------------------------------------
  3. ----------------------- GLOBAL VARIABLES ------------------------
  4. -----------------------------------------------------------------
  5. -----------------------------------------------------------------*/
  6.  
  7.  
  8. // Used to add a numeric id on slide creation to let us target the element later
  9. var slideIndex = 0;
  10. // Tells us which slide we are on
  11. var currentSlideIndex = 0;
  12. // An Array to hold all the slides
  13. var slideArray = [];
  14.  
  15.  
  16. /*-----------------------------------------------------------------
  17. -----------------------------------------------------------------
  18. ----------------------- THE TEMPLATE ---------------------------
  19. -----------------------------------------------------------------
  20. -----------------------------------------------------------------*/
  21.  
  22.  
  23.  
  24. // Template for creating a custom Slide object
  25. function Slide(title, subtitle, background, link ) {
  26. this.title = title;
  27. this.subtitle = subtitle;
  28. this.background = background;
  29. this.link = link;
  30. // we need an id to target later using getElementById
  31. this.id = "slide" + slideIndex;
  32. // Add one to the index for the next slide number
  33. slideIndex++;
  34. // Add this Slide to our array
  35. slideArray.push(this);
  36. }
  37.  
  38.  
  39.  
  40. /*-----------------------------------------------------------------
  41. -----------------------------------------------------------------
  42. ----------------------- SLIDE CREATION ---------------------------
  43. -----------------------------------------------------------------
  44. -----------------------------------------------------------------*/
  45.  
  46.  
  47. // Creating our slide objects, you can make as many as you want
  48.  
  49. var walkingDead = new Slide(
  50. "The Walking Dead",
  51. "A show about fighting zombies",
  52. "https://source.unsplash.com/450x450/?girl",
  53. "#"
  54. );
  55.  
  56. var bigBang = new Slide(
  57. "The Big Bang Theory",
  58. "A show about Sheldon",
  59. "https://source.unsplash.com/450x450/?cat",
  60. "#"
  61. );
  62.  
  63. var LastMan = new Slide(
  64. "The Last Man on Earth",
  65. "A show about loneliness",
  66. "https://source.unsplash.com/450x450/?tech",
  67. "#"
  68. );
  69.  
  70.  
  71.  
  72. /*-----------------------------------------------------------------
  73. -----------------------------------------------------------------
  74. ----------------------- ADD TO WEB PAGE ---------------------------
  75. -----------------------------------------------------------------
  76. -----------------------------------------------------------------*/
  77.  
  78.  
  79.  
  80. function buildSlider(){
  81. // A variable to hold all our HTML
  82. var myHTML;
  83. // Go through the Array and add the code to our HTML
  84. for(var i = 0; i < slideArray.length; i++) {
  85. myHTML += "<div id='" + slideArray[i].id +
  86. "' class='singleSlide' style='background-image:url(" + slideArray[i].background + ");'>" +
  87. "<div class='slideOverlay'>" +
  88. "<h1>" + slideArray[i].title + "</h1>" +
  89. "<h4>" + slideArray[i].subtitle + "</h4>" +
  90. "<a href='" + slideArray[i].link + "' target='_blank'>Open Link</a>" +
  91. "</div>" +
  92. "</div>";
  93. }
  94. // Print our HTML to the web page
  95. document.getElementById("mySlider").innerHTML = myHTML;
  96. // Display the first slide
  97. document.getElementById("slide" + currentSlideIndex).style.left = 0;
  98.  
  99. }
  100.  
  101. // Create our slider
  102. buildSlider();
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /*-----------------------------------------------------------------
  109. -----------------------------------------------------------------
  110. ----------------------- SLIDER CONTROLS ---------------------------
  111. -----------------------------------------------------------------
  112. -----------------------------------------------------------------*/
  113.  
  114.  
  115.  
  116. // Navigates to the previous slide in the list
  117. function prevSlide(){
  118. // Figure out what the previous slide is
  119. var nextSlideIndex;
  120. // If we are at zero go to the last slide in the list
  121. if (currentSlideIndex === 0 ) {
  122. nextSlideIndex = slideArray.length - 1;
  123. } else {
  124. // Otherwise the next one is this slide minus 1
  125. nextSlideIndex = currentSlideIndex - 1;
  126. }
  127. // Setup the next slide and current slide for animations
  128. document.getElementById("slide" + nextSlideIndex).style.left = "-100%";
  129. document.getElementById("slide" + currentSlideIndex).style.left = 0;
  130. // Add the appropriate animation class to the slide
  131. document.getElementById("slide" + nextSlideIndex).setAttribute("class", "singleSlide slideInLeft");
  132. document.getElementById("slide" + currentSlideIndex).setAttribute("class", "singleSlide slideOutRight");
  133. // Set current slide to the new current slide
  134. currentSlideIndex = nextSlideIndex;
  135. }
  136.  
  137.  
  138. // Navigates to the next slide in the list
  139. function nextSlide(){
  140. // Figure out what the next slide is
  141. var nextSlideIndex;
  142. // If we are at the last slide the next one is the first (zero based)
  143. if (currentSlideIndex === (slideArray.length - 1) ) {
  144. nextSlideIndex = 0;
  145. } else {
  146. // Otherwise the next slide is the current one plus 1
  147. nextSlideIndex = currentSlideIndex + 1;
  148. }
  149. // Setup the next slide and current slide for animations
  150. document.getElementById("slide" + nextSlideIndex).style.left = "100%";
  151. document.getElementById("slide" + currentSlideIndex).style.left = 0;
  152. // Add the appropriate animation class to the slide
  153. document.getElementById("slide" + nextSlideIndex).setAttribute("class", "singleSlide slideInRight");
  154. document.getElementById("slide" + currentSlideIndex).setAttribute("class", "singleSlide slideOutLeft");
  155. // Set current slide to the new current slide
  156. currentSlideIndex = nextSlideIndex;
  157. }

That’s all! hopefully, you have successfully integrated this responsive image slider with text code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Connect with us on social media:

1 thought on “Responsive Image Slider with Text”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X