.

.

Sunday, June 9, 2019

Roshan Thpa

Pure CSS Moving Cloud Animation : with Source code

In this post, you will be looking at a really cool animation of moving clouds, like the one shown in the following demo.
Nowadays, many websites have animations of moving images. This one is no different. These kind of animations are pretty easy to code which I will be showing you in three simple steps.

Step 1

For this animation, I took an image of clouds and animated it. Make a div and give the cloud image as its background image. Then, enclose the div within another div which will serve as the wrapper div.
HTML
<div id="sky">
 <div id="clouds"></div>
</div>
CSS
#clouds {
 background-image: url('pic.jpg');
 background-size: cover;
}

Step 2

Give the image div width 200% so that we see only 100% (i.e. half of the image) on the screen at a time, to see a continuous image while the image scrolls.
Give overflow: hidden to the outer div to prevent horizontal scroll bar (since the image div width is twice the screen width).
CSS
#sky {
 overflow: hidden;
}
#clouds {
 width: 200%;
 height: 400px;
 background-image: url('pic.jpg');
 background-size: cover;
}

Step 3

Now it's time for animation. For that, @keyframes rule is used. The idea is to place the left end of the image at margin-left: 0px at the start of animation and at margin-left: -100% at the end of animation. As an effect of this, the first half of the image is visible as the animation starts, and the second half of the image is visible as the animation is 100% complete.
Finally, to make the image last forever, give the value infinite to the property animation. I named the animation 'movingclouds' and the animation duration 25 sec. The more the animation duration, lesser will be the speed of animation.
So, the final code is given below.
HTML
<div id="sky">
 <div id="clouds"></div>
</div>
CSS
#sky {
 overflow: hidden;
}
#clouds {
 width: 200%;
 height: 400px;
 background-image: url('pic.jpg');
 background-size: cover;
 -webkit-animation: movingclouds 25s linear infinite;
 -moz-animation: movingclouds 25s linear infinite;
 -o-animation: movingclouds 25s linear infinite;
}
@keyframes movingclouds {
 0% {margin-left: 0%;}
 100% {margin-left: -100%;}
}

Roshan Thpa

About Roshan Thpa -

Roshan Kumar Thapa is veteran keen tech-savvy person which has enabled him to qualify for the job. He has knowledge in wide range of IT fields. He is highly trained and skilled in Graphic design, Tally, A+ hardware and networking, AutoCAD, Web Design/Development, Application Development, Video editing, Q-Basic and had good knowledge of C, C++, C# and Java programming. He keeps a keen interest in information technology and loves to keep himself updated through news, magazines, books, and blogs. He likes to learn and share his knowledge. He also runs a blog where he posts updates about the latest advancements in technology and his own teachings as well.

Subscribe to this Blog via Email :