JavaScript - Creating Image Carousel
Submitted by razormist on Sunday, September 1, 2019 - 15:16.
In this tutorial we will create a Creating Image Carousel using JavaScript. This code can dynamically change the image when the user click the button. The code use onclick() to launch a function that can automatically change the image within a set of duration using setInterval(), and also can change the image using button by clicking it will increment or decrement the array index position of an image. Feel free to modify and apply it in your system, this is a user-friendly kind of program
We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.
There you have it we successfully created a Creating Image Carousel using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting started:
First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- </head>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <body>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="row">
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will dynamically change the image display when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.- var image = new Array("images/image1.jpeg", "images/image2.jpg", "images/image3.jpeg");
- var count = 0;
- var len = image.length - 1;
- autoPlay();
- function autoPlay(){
- setInterval("imageCarousel(1)", 4000);
- }
- function imageCarousel(index){
- count = count + index;
- if(count > len){
- count = 0;
- }
- if(count < 0){
- count = len;
- }
- document.slideshow.src = image[count];
- return false;
- }
Add new comment
- 382 views