Click here to Skip to main content
15,879,613 members
Articles / Programming Languages / Javascript
Tip/Trick

Image Sliding Effect with JavaScript

Rate me:
Please Sign up or sign in to vote.
4.95/5 (7 votes)
9 Apr 2014CPOL 89.2K   2.4K   11   6
Quick way to insert an image sliding effect on your web site

Introduction

This tip is meant to give others a shortcut on how to get quick image sliding effect on the webpage without much trouble and no JQuery.

Background

I came across this site while looking for a way to animate a group of images. And from all my options, I found it to be the simplest one. After researching html 5 a very simple option, although not the effect was also quite simple, was possible with the help of canvas and javascript.

1. Using menu cool

Download their folder and place the file in your project. Then add:

HTML
<link href="js-image-slider.css" rel="stylesheet" type="text/css" />
<script src="js-image-slider.js" type="text/javascript"></script>

Inside your page, add/type the following div block:

HTML
<div id="sliderFrame">
<div id="slider">
    <a href="http://www.menucool.com/javascript-image-slider"
    target="_blank">
        <img src="images/image-slider-1.jpg"
        alt="Welcome to Menucool.com" />
    </a>
    <img src="yourImage"
    alt="This will show on yourImage caption" />
    <img src="yourOtherImage"
    alt="This will show on yourOtherImage caption" />
    <img src="yourOtherOTherImage" alt="#htmlcaption" />
</div>
<div id="htmlcaption" style="display: none;">
    <p>"This will show on yourOtherOTherImage caption"</p>
</div>
</div> 

2. Using HTML 5

On your html page have a canvas, if with assign an Id, even better

HTML
<canvas id="theCanvas">

At the place you have your javascript for the page

JavaScript
//the array with the path to the images
var pathToYourImages = ["pathToimage.imageExt", "pathToimage2.imageExt", "pathToimage3.imageExt", ....];

//a variable to hold the canvas element
var canvas = null;

//a variable to hold the canvas context
var contextForCanvas = null;
    
//a variable for the image element to be draw
var image = null;

//a variable to control which image to be displayed.
var currentImage = 0;

//when the wondow.onload happen, assing the value to the variables and then call ImageSliding.
window.onload = function() {
    canvas = document.getElementById('theCanvas');
    contextForCanvas = canvas.getContext('2d');
    image = document.createElement("img");

    ImageSliding();
};

//Performs the change on the image path and controls which will be showed
function ImageSliding() {

    //sets the image's path
    image.setAttribute('src', pathToYourImages[currentImage]);

    //control which image is being used
    currentImage = currentImage === (pathToYourImages.length - 1) ? 0 : ++currentImage;

    //sets the transparency value
    contextForCanvas.globalAlpha = 0.1;
            
    //calls the timeout function
    setTimeout(ShowImage, 200);
}

//clears the canvas and show the new image
function ShowImage() {

    contextForCanvas.save();

    contextForCanvas.clearRect(0, 0, canvas.width, canvas.height);
    contextForCanvas.drawImage(image, 0, 0, 300, 150);
    contextForCanvas.globalAlpha += 0.1;

    contextForCanvas.globalAlpha > 0.9 ? setTimeout(ImageSliding, 3500) : setTimeout(ShowImage, 200);
}

Conclusion

Basically, these are two ways to get the effect. It's advisable to always research in more than one place and evaluate what would be the best solution for your problem. I hope this helps.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
I work from home as a business owner and Front-end developer of a company whose main revenue comes from a SaaS we created to help people manage their online sales on a website called Mercado Livre. On my spare time I’ve been building a website called www.mundojs.com.br where I provide information about JavaScript in Portuguese to help new developers.

Comments and Discussions

 
Questionnice joke Pin
Member 1171713025-May-15 3:19
Member 1171713025-May-15 3:19 
QuestionEffect Pin
Member 1116361018-Oct-14 12:34
Member 1116361018-Oct-14 12:34 
QuestionSlideshow Pin
Member 105135976-Apr-14 10:23
Member 105135976-Apr-14 10:23 
AnswerRe: Slideshow Pin
Paulo Augusto Kunzel9-Apr-14 1:43
professionalPaulo Augusto Kunzel9-Apr-14 1:43 
AnswerRe: Slideshow Pin
Christian Amado9-Apr-14 2:04
professionalChristian Amado9-Apr-14 2:04 
GeneralRe: Slideshow Pin
Paulo Augusto Kunzel9-Apr-14 2:10
professionalPaulo Augusto Kunzel9-Apr-14 2:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.