Advertisement
mauricemuteti

How To Make Image SlideShow Using Javascript

Apr 5th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Slider</title>
  5. <link rel="stylesheet" href="style.css"/>
  6. <link rel="stylesheet" href="fontawesome/css/font-awesome.css"/>
  7. <script style="text/javascript" src="javakl.js"></script>
  8.  
  9. </head>
  10. <body>
  11.  
  12. <div id="container">
  13.  
  14. <p id="pic"></p>
  15.  
  16. <div id="sliderdiv">
  17. <img src="images/Chrysanthemum.jpg" height="400px" width="900px" id="imagethmb"/>
  18.  
  19. <div id="buttons">
  20. <a href="#" onclick="startslide(1)" > > </a>
  21. <img src="images/next.png" onclick="startslide(1)" height="70px" width="70px" class="next"\/>
  22. <img src="images/previous.png" onclick="startslide(-1)" height="70px" width="70px" class="previous"/>
  23. </div>
  24.  
  25. <div id="caption">
  26. <p id="cap">caption</p>
  27. </div>
  28.  
  29. </div>
  30. </div>
  31. </body>
  32. </html>
  33.  
  34. Javascript Source Code
  35.  
  36. var images = [
  37. "images/Chrysanthemum.jpg",
  38. "images/Desert.jpg",
  39. "images/Hydrangeas.jpg",
  40. "images/Jellyfish.jpg",
  41. "images/Koala.jpg"
  42. ];
  43.  
  44. var caption = [
  45. "caption 1",
  46. "caption 2",
  47. "caption 3",
  48. "caption 4",
  49. "caption 5"
  50. ];
  51.  
  52. var imagePos = 0;
  53. var imagesLenth = images.length - 1;
  54.  
  55. function startslide(x){
  56. imagePos += x;
  57.  
  58. if(imagePos > imagesLenth) {
  59. imagePos =0;
  60. }
  61.  
  62. if(imagePos < 0) {
  63. imagePos = imagesLenth;
  64. }
  65.  
  66. document.getElementById('imagethmb').src = images[imagePos];
  67. document.getElementById('cap').innerHTML = caption[imagePos];
  68.  
  69. }
  70.  
  71. setInterval(function startslide(){
  72. imagePos ++;
  73.  
  74. if(imagePos > imagesLenth) {
  75. imagePos =0;
  76. }
  77.  
  78. if(imagePos < 0) {
  79. imagePos = imagesLenth;
  80. }
  81.  
  82. document.getElementById('imagethmb').src = images[imagePos];
  83. document.getElementById('cap').innerHTML = caption[imagePos];
  84.  
  85. },1000);
  86.  
  87. Css
  88. Source Code
  89.  
  90. *{
  91. padding: 0px;
  92. margin: 0px;
  93. }
  94.  
  95. #container {
  96. width:900px;
  97. height: auto;
  98. margin-left: auto;
  99. margin-right: auto;
  100. }
  101.  
  102. #sliderdiv {
  103. height: 400px;
  104. width: 100%;
  105. position:relative;
  106. }
  107.  
  108. #left {
  109. height:80px;
  110. width:80px;
  111. }
  112.  
  113. #buttons {
  114. height:100px;
  115. width:100%;
  116. position:absolute;
  117. top:28%;
  118.  
  119. }
  120.  
  121. .next {
  122. float:right;
  123. line-height:100px;
  124. }
  125.  
  126. .previous {
  127. float:left;
  128. vertical-align:center;
  129. }
  130.  
  131. #caption {
  132. height:100px;
  133. width:100%;
  134. background-color:black;
  135. position:absolute;
  136. opacity:0.5;
  137. bottom:0;
  138. color:white;
  139. }
  140. // video tutorial  https://www.youtube.com/watch?v=f-nUv4HPS8Q
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement