By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > HTML & CSS > Moving Car Animation using CSS and JavaScript
HTML & CSS

Moving Car Animation using CSS and JavaScript

Rohan750
Last updated: 2021/05/16 at 5:08 AM
Rohan750
Share
3 Min Read

This is fun tutorial, we will see how you can animate a car with simple CSS and move it. We use plain HTML and CSS to animate our car. We animate roads, buildings and car wheels.We will learn how to drive a car with CSS and add more functionality using JavaScript like car sound.We are going to create this tutorial to understand how animation works.

Contents
HTML Code </html> css file Javascript code

You see online:

https://caranimatedusingcssandjs.netlify.app/

HTML Code

Moving Car Animation using  CSS and javascript :First we create our HTML file and then we add all the images using the HTML image tag. We have pictures of the wheels and car body.
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <meta name="viewport" content="width=device-width, initial-scale=1.0">  
   <link rel="stylesheet" href="style.css">  
   <script src="script.js"></script>  
   <link rel="icon" href="https://img.icons8.com/color/48/000000/lamborghini.png" type="image/x-icon">  
   <title>Car Animation</title>  
 </head>  
 <body>  
   <div class="container">  
     <div class="sky">  
       <div class="trees"></div>  
       <div class="track"></div>  
       <div class="car">  
         <div class="wheel wheel1">  
           <img src="car_wheel_left.png" alt="">  
         </div>  
         <div class="wheel wheel2">  
           <img src="car_wheel_right.png" alt="">  
         </div>  
       </div>  
     </div>  
   </div>  
 </body>  

</html>

css file

*{
    margin:0 ;
    padding: 0;
}
body{
    overflow: hidden;
    animation: shakebody linear 6s infinite;
    background-image: url(background.jpg);
}
.sky{
    height: 100vh;
    width: 100%;
    background-image: url(background.jpg);
    background-repeat: no-repeat;
    position: absolute;
}
.trees{
    height: 100vh;
    width: 100%;
    background-image: url(trees.png);
    background-size: cover; 
    position: absolute;
    top: -144px; 
}
.track{
    height:60vh;
    width:800vw;
    background-image: url(track.png);
    position:absolute;
    top:70vh;
    background-repeat: repeat-x; 
    animation: carMove linear 13s infinite;
}
.car{
    height: 100px;
    width: 380px;
    background-image: url(car_body.png);
    background-size: cover;
    background-repeat: no-repeat;
    position: absolute;
    left: 444px;
    bottom:30vh;
    animation: shake linear .3s infinite;
}
.wheel1 img{
    width: 77px;
    position: relative;
    top:42px;
    left:42px;
    animation: wheelRotation linear .16s infinite;
}


.wheel2 img{
    width: 77px;
    position: relative;
    top: -39px;
    left: 235px;
    animation: wheelRotation linear .16s infinite;

}
@keyframes wheelRotation
{
    100%{
        transform: rotate(360deg);
    }
}
@keyframes carMove
{
    100%{
        transform: translateX(-500vw);
    }
}
@keyframes shake
{
    0%{
        transform: translateY(-5px);
    }
    50%{
        transform: translateY(5px);
    }
    100%{
        transform: translateY(-5px);
    }
}
@keyframes shakebody
{
    0%{
        transform: translateY(-50px);
    }
    50%{
        transform: translateY(50px);
    }
    100%{
        transform: translateY(-50px);
    }
}
@media only screen and (max-width: 768px) {
    .car{
        height: 50px;
        width: 190px;
        left: 180px;
        position: absolute;
    }
    .wheel1 img{
        width: 38.5px;
        position: relative;
        top:22px;
        left:22px;
    }.wheel2 img{
        width: 39px;
        position: relative;
        top: -21px;
        left: 115px;
    }
}

Javascript code

In javascript code we can add car sound 


var audio = document.createElement('audio');
audio.setAttribute('src', 'sound.mp3'); 
audio.loop=true;
audio.play();


Full source code:

https://github.com/patelrohan750/caranimated.github.io

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love2
Sad0
Happy0
Sleepy0
Angry2
Dead1
Wink1
Previous Article 10 tips for learning python fast! master in python
Next Article Hangman in Python
22 Comments 22 Comments
  • Vickie says:
    August 17, 2021 at 1:22 am

    I think the admin of this website is really working hard for his web site, for
    the reason that here every material is quality based
    information.

    Reply
  • lullabies says:
    October 18, 2021 at 4:57 pm

    free lullabies

    Reply
  • Chad says:
    November 11, 2021 at 10:32 pm

    Great delivery. Great arguments. Keep up the great spirit.

    Reply
  • Joni says:
    November 17, 2021 at 12:48 am

    Greetings! Very helpful advice in this particular article!
    It is the little changes that make the biggest changes.
    Many thanks for sharing!

    Reply
  • Ginger says:
    November 18, 2021 at 11:58 pm

    First off I would like to say excellent blog! I had a quick question that I’d like to ask if you do not mind.

    I was curious to find out how you center yourself and clear your
    thoughts prior to writing. I have had difficulty clearing my thoughts in getting my ideas out.
    I do take pleasure in writing but it just seems like the first 10 to 15 minutes are lost just trying to
    figure out how to begin. Any recommendations or hints? Thanks!

    Reply
  • Carlo says:
    December 11, 2021 at 4:38 am

    Fastidious respond in return of this issue with genuine arguments and explaining everything about
    that.

    Reply
  • Chelsey says:
    December 14, 2021 at 11:17 pm

    Informative article, totally what I wanted to find.

    Reply
  • Arlie says:
    December 16, 2021 at 6:35 am

    Hurrah! After all I got a website from where I be able to actually take useful information regarding my study and knowledge.

    Reply
  • ทางเข้าSUPERSLOT says:
    March 8, 2022 at 5:22 pm

    A round of applause for your blog.Thanks Again. Great.

    Reply
  • tinyurl.com says:
    March 23, 2022 at 2:32 am

    I’m really impressed with your writing skills as
    well as with the layout on your weblog. Is this a paid theme or did you modify it
    yourself? Either way keep up the excellent quality writing, it is
    rare to see a great blog like this one these days.

    Reply
  • bit.ly says:
    March 23, 2022 at 5:27 pm

    Hello, There’s no doubt that your blog may be having internet browser
    compatibility issues. When I take a look at your website
    in Safari, it looks fine however when opening in IE, it’s got some overlapping issues.

    I just wanted to provide you with a quick heads up!
    Other than that, excellent blog!

    Reply
  • t.co says:
    March 24, 2022 at 7:56 am

    I loved as much as you’ll receive carried out right here. The sketch is
    tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following.
    unwell unquestionably come further formerly again as exactly the same
    nearly very often inside case you shield this hike.

    Reply
  • bit.ly says:
    March 24, 2022 at 5:59 pm

    Appreciate the recommendation. Let me try it out.

    Reply
  • product review says:
    April 24, 2022 at 3:26 pm

    I love how your website looks, thanks for the post it was nice read.

    Reply
  • product review says:
    April 26, 2022 at 6:59 pm

    Excellent goods from you, man. I have bear in mind your stuff previous to and you’re just too great. I really like what you have acquired right here, really like what you’re stating and the best way during which you are saying it. You’re making it entertaining and you continue to care for to keep it smart. I can’t wait to read much more from you. That is actually a great web site.

    Reply
  • vaskir says:
    May 2, 2022 at 10:15 am

    Nice use of gold in this concept, friend.

    Reply
  • video product review says:
    May 2, 2022 at 10:21 am

    Really love your website design! Have you checked my wordpress website, do you like the theme?

    Reply
    • Admin says:
      May 8, 2022 at 7:24 am

      Please share your website

      Reply
  • product review says:
    May 10, 2022 at 11:17 am

    This is radiant work, friend.

    Reply
  • product review says:
    May 12, 2022 at 12:35 pm

    I conceive this site has some real excellent info for everyone :D. “Time–our youth–it never really goes, does it It is all held in our minds.” by Helen Hoover Santmyer.

    Reply
  • Winfred Kallhoff says:
    May 22, 2022 at 8:31 pm

    Hi there, simply was aware of your blog through Google, and located that it is really informative. I am gonna be careful for brussels. I will be grateful for those who proceed this in future. A lot of other folks will probably be benefited from your writing. Cheers!

    Reply
  • houston junk car buyer says:
    May 25, 2022 at 6:28 pm

    It’s going to be ending of mine day, but before end I am reading this fantastic
    paragraph to improve my knowledge.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Price Range Slider

How to Make Price Range Slider Using JavaScript

December 8, 2022
save text as File in HTML

How to Save Text as File in JavaScript

December 5, 2022
Calendar in HTML

How to Make Dynamic Calendar in HTML & JavaScript

December 5, 2022
instagram signup form

How to Make Instagram Signup Page In HTML and CSS

December 5, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?