In this article, we are given some images and the task is the create a slow transition from one image to another using Javascript.
Prerequisite: In this article, we will use the following JavaScript methods.
Approach: Given some images and the task is to change the images after regular intervals with a fading or dissolving effect. To change the image at regular intervals using the setInterval() method. Keep the images on top of each other and keep moving the topmost image to the bottom by changing its z-index at regular intervals.
To make the image transition with a fading effect we use the asynchronous function. Inside the asynchronous function, use another setInterval() method to slowly decrease the opacity of the topmost image till opacity becomes 0. By doing this, the topmost image will appear to fade away slowly. Once, the topmost image is completely faded away, move it to the bottom-most position and store the new top image index.
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>
Change Image Dynamically when User Scrolls
</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<b>A Computer Science Portal for Geeks</b>
<div id="scroll-image">
<img src=
class="test" />
<img src=
class="test" />
<img src=
class="test" />
</div>
</body>
</html>
|
CSS
body {
text-align: center;
}
h1 {
color: green;
}
img {
position: absolute;
left: 400px;
}
|
javascript
<script>
startImageTransition();
function startImageTransition() {
var images = document.getElementsByClassName("test");
for (var i = 0; i < images.length; ++i) {
images[i].style.opacity = 1;
}
var top = 1;
var cur = images.length - 1;
setInterval(changeImage, 3000);
async function changeImage() {
var nextImage = (1 + cur) % images.length;
images[cur].style.zIndex = top + 1;
images[nextImage].style.zIndex = top;
await transition();
images[cur].style.zIndex = top;
images[nextImage].style.zIndex = top + 1;
top = top + 1;
images[cur].style.opacity = 1;
cur = nextImage;
}
function transition() {
return new Promise(function (resolve, reject) {
var del = 0.01;
var id = setInterval(changeOpacity, 10);
function changeOpacity() {
images[cur].style.opacity -= del;
if (images[cur].style.opacity <= 0) {
clearInterval(id);
resolve();
}
}
})
}
}
</script>
|
Output: Click here to check the live output.

Note: The time interval at which images are changing should be greater than the time it takes to make the opacity of the image less than or equals to 0.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
07 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...