Simple jQuery Slideshow tutorial

How to Create a simple Image Slider using jQuery


 

In this tutorial, we are going to learn how to add a simple slide show to an HTML Page. This works with a basic jQuery fade-in and fade-out functions. No plugin is used. To add this, just follow the given instructions. 

1. Create HTML file
2. Add jQuery  CDN link
3. Add some images
4. Add style code.
5. Add  jQuery snippet.
6. Run File.


1. So first,  create a file with the name  index.html and paste  the given code below, and save it. 

 

index.html

<!DOCTYPE html> 
 
<html>
<head>
<title> Image Slider Demo</script>
<script type="text/javascript" src=""></script>


<style>

</style>


<script type="text/javascript"> 
 ...
 
</script>

</head>
 
 
<body>

 
<div id="slider">
 
   <div>
     <img src="">
   </div>
   <div>
     <img src="">
   </div>
   <div>
     <img src="">
   </div>

</div>

</body>
</html>

 TOC

2. Add some image tags in a each of div block, inside the slider div: ex:

    <div>
     <img src="images/1.jpg">
   </div>

 

For this, create one images folder and add some images and link in each div block. 

3. Add the given jQuery  CDN link in the first script tag  src.

https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js

4. Add style code.

Next, add the given CSS code in style tags specified in a HTML page

 

#slider { 
    position: relative;     
    margin: 50px auto; 
    padding: 10px
    width: 600px; 
    height: 230px; 
    box-shadow: 0 0 10px rgba(0,0,0,0.4);
   
}

#slider > div { 
    position: absolute; 
    top: 10px; 
    left: 10px; 
    right: 10px; 
    bottom: 10px; 
}
 
 

5. Add a jQuery snippet. 


Add the following  jQuery snippet code  in a given script  tags. Replace with 3 dots(...) in a script tags given in index page.


setInterval(function() { 
  $('#slider > div:first')
    .fadeOut(1000).next()
    .fadeIn(1000).end()
    .appendTo('#slider');
},  2000);
 
$("#slider > div:gt(0)").hide();
 
 
 

6. Save the file and open it in browser.

 

Now, the full HTML code looks as follows:

  TOCMenu

 

index.html

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<style>
#slider { 
    position: relative;     
    margin: 50px auto;     
    padding: 10px     
    width: 600px; 
    height: 230px; 
    box-shadow: 0 0 10px rgba(0,0,0,0.4);
   
}

#slider > div { 
    position: absolute; 
    top: 10px; 
    left: 10px; 
    right: 10px; 
    bottom: 10px; 
}
 
</style>


<script type="text/javascript"> 

setInterval(function() { 
  $('#slider > div:first')
    .fadeOut(1000).next()
    .fadeIn(1000).end()
    .appendTo('#slider');
},  2000);
 
$("#slider > div:gt(0)").hide();

</script>

</head>
<body>

<div id="slider">
 
   <div>
     <img src="https://picsum.photos/600/230">
   </div>
   <div>
     <img src="https://picsum.photos/id/237/600/230">
   </div>
   <div>
     <img src="https://picsum.photos/600/230">
   </div>

</div>

</body>
</html>

Note: You can customize height and width of the slider according to your choice, by changing the CSS height and width properties values.

 

Output:

coding-zon-jquery-slider-demo
 


 

 


Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP