How To Create Image Carousel
Submitted by alpha_luna on Friday, April 8, 2016 - 13:11.
In this tutorial, we are going to learn about on How To Create Image Carousel. Now, we’re creating a beautiful image carousel using jQuery.
In this source code, I have five (5) images to run for the carousel source code. The feature of this tutorial has a next and previous button for circling the image back to back.
So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you.
Directions:
Image Carousel HTML
In this code below, this is HTML code for our five (5) images including our next and previous button controls.- <center>
- <div id="carousel_image" style="margin:50px;">
- <img class="start_image" src="image/33.png" style="margin-left:-275px;">
- <img src="image/22.jpg" style="margin-left:-275px;">
- <img src="image/11.png" style="margin-left:-275px;">
- <img src="image/44.jpg" style="margin-left:-275px;">
- <img src="image/55.jpg" style="margin-left:-275px;">
- <div class="carousel_button" id="previous_image" style="margin-left:-260px; margin-top:100px;">
- <input type="button" value="Previous">
- </div>
- <div class="carousel_button" id="next_image" style="margin-right:-255px; margin-top:100px;">
- <input type="button" value="Next">
- </div>
- </div>
- </center>
Image Carousel jQuery
This script function is used for circling the image back to back. When we click our controls.- <script src="js/code_js.js"></script>
- <script>
- $(document).ready(function() {
- $(".start_image").show();
- $("#carousel_image").hover(function(){$(".carousel_button").show();},function(){$(".carousel_button").hide();})
- $(".carousel_button").on('click',function(){
- var id = $(this).attr('id');
- var nav;
- if(id=="previous_image") {
- nav = $("img.start_image").prev('img');
- if(nav.length == 0) nav = $("img").last();
- } else if(id=="next_image") {
- nav = $("img.start_image").next('img');
- if(nav.length == 0) nav = $("img").first();
- }
- $("img.start_image").hide();
- $("img.start_image").removeClass("start_image");
- nav.addClass("start_image");
- nav.fadeIn(1000);
- });
- });
- </script>
Image Carousel CSS
This is the CSS code for our Image Carousel.- <style type="text/css">
- .carousel_button {
- display:none;
- cursor:pointer;
- position: absolute;
- top: 100px;
- opacity: 0.8;
- padding: 10px;
- background: #333;
- color: #FFF;
- font-weight: bold;
- font-size: 20px;
- }
- #previous_image {
- left:0px;
- }
- #next_image {
- right:0px;
- }
- #carousel_image img {
- display:none;
- }
- #carousel_image {
- position:relative;
- width:300px;
- height:200px;
- }
- </style>
Add new comment
- 2122 views