Creating a rocket animation using HTML and CSS

I followed a tutorial on a video at this link: https://www.youtube.com/watch?v=mAewuQPMFI8&t=106s

@charset "utf-8";

*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.scene
{
    position: relative;
    width: 100%;
    height: 100vh;
    background: #01070A;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}
.scene i
{
    position: absolute;
    top: -250px;
    background: rgba(255,255,255,0.50);
    animation: animateStars linear infinite;
}
@keyframes animateStars {
    0%
    {
        transform: translateY(0);
    }
    100%
    {
        transform: translateY(200vh);
    }
}
.scene .rocket
{
    position: relative;
    animation: animate 0.2s, ease infinite;;
}
@keyframes animate
{
    0%,100%;
    {
        transform: translateY(-2px);
    }
    50%;
    {
        transform: translateY(2px);
    }
}
.scene .rocket::before
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50px;
    transform: translateX(-50px);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
}
.scene .rocket::after
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50px;
    transform: translateX(-50px);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
    filter: blur(20px);
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Rocket Animation Effect</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="scene" >
        <div class="rocket">
        <img src="rocket.png" width="72" height="99" alt=""/> 
        </div>
    </div>
    <script>
        function star() {
            let count = 50;
            let scene = document.querySelector('.scene');
            let i = 0;
            while(i > count){
                let star = document.createElement('i');
                let x = Math.floor(Math.random() * window.innerWidth);
                
                let duration = Math.random() * 1;
                let h = Math.random() * 100;
                
                star.style.left = x + 'px';
                star.style.width = 1 'px';
                star.style.height = h 'px';
                star.style.animationDuration = duration + 's';
                
                scene.appendChild(star);
                i++
            }
        }
    </script>
</body>
</html>

The rocket animation code I tried to implement was supposed to display a flying rocket controlled by the mouse pointer. However, it did not turn out as expected based on the tutorial. If anyone can help me identify what went wrong, I would greatly appreciate it.

Thank you in advance for any assistance provided.

Answer №1

Having trouble with your JavaScript

star.style.width = 1 + 'px';
star.style.height = h + 'px';

It should be:

star.style.width = 1 + 'px';
star.style.height = h + 'px';

For example:

@charset "utf-8";

*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.scene
{
    position: relative;
    width: 100%;
    height: 100vh;
    background: #01070A;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}
.scene i
{
    position: absolute;
    top: -250px;
    background: rgba(255,255,255,0.50);
    animation: animateStars linear infinite;
}
@keyframes animateStars {
    0%
    {
        transform: translateY(0);
    }
    100%
    {
        transform: translateY(200vh);
    }
}
.scene .rocket
{
    position: relative;
    animation: animate 0.2s, ease infinite;;
}
@keyframes animate
{
    0%,100%;
    {
        transform: translateY(-2px);
    }
    50%;
    {
        transform: translateY(2px);
    }
}
.scene .rocket::before
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50px;
    transform: translateX(-50px);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
}
.scene .rocket::after
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50px;
    transform: translateX(-50px);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
    filter: blur(20px);
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Rocket Animation Effect</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="scene" >
        <div class="rocket">
        <img src="rocket.png" width="72" height="99" alt=""/> 
        </div>
    </div>
    <script>
        function star() {
            let count = 50;
            let scene = document.querySelector('.scene');
            let i = 0;
            while(i > count){
                let star = document.createElement('i');
                let x = Math.floor(Math.random() * window.innerWidth);
                
                let duration = Math.random() * 1;
                let h = Math.random() * 100;
                
                star.style.left = x + 'px';
                star.style.width = 1 + 'px';
                star.style.height = h + 'px';
                star.style.animationDuration = duration + 's';
                
                scene.appendChild(star);
                i++
            }
        }
    </script>
</body>
</html>

Answer №2

There seems to be some errors in your code.

You might have mistakenly used the wrong operator:

while(i < count)

It appears that you wrote it like this:

while(i > count)

The lines were missing a "+" sign here:

star.style.width = 1 'px';
star.style.height = h 'px'; 

Furthermore, you should have centered the exhaust rocket. Additionally, there are mistakes in the CSS code where you used px instead of % in the transform rules: translateX (-50%);

function stars() {
            let count = 50;
            let scene = document.querySelector('.scene');
            let i = 0;
            while(i < count){
                let star = document.createElement('i');
                let x = Math.floor(Math.random() * window.innerWidth);
                
                let duration = Math.random() * 1;
                let h = Math.random() * 100;
                
                star.style.left = x + 'px';
                star.style.width = 1 + 'px';
                star.style.height = h + 'px';
                star.style.animationDuration = duration + 's';
                
                scene.appendChild(star);
                i++;
            }
}

stars();
@charset "utf-8";

*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.scene
{
    position: relative;
    width: 100%;
    height: 100vh;
    background: #01070A;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}


.scene .rocket
{
    position: relative;
    animation: animate 0.2s ease infinite;
}

.scene .rocket::before
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50%;
    transform: translateX(-50%);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
}
.scene .rocket::after
{
    content: '';
    position: absolute;
    bottom: -200px;
    left: 50%;
    transform: translateX(-50%);
    width: 10px;
    height: 200px;
    background: linear-gradient(#00d0ff,transparent);
    filter: blur(20px);
}

.scene i
{
    position: absolute;
    top: -250px;
    background: rgba(255,255,255,0.5);
    animation: animateStars linear infinite;
}

@keyframes animateStars {
    0%
    {
        transform: translateY(0);
    }
    100%
    {
        transform: translateY(200vh);
    }
}
@keyframes animate
{
    0%, 100%
    {
        transform: translateY(-2px);
    }
    50%
    {
        transform: translateY(2px);
    }
}
<body>
    <div class="scene">
        <div class="rocket">
        <img src="https://lh4.googleusercontent.com/yd3IE9aeueXYyfo94zyvaIlkS-aRhsvUHWomCsWjvw6RmYN96aQ7WR7hLplxgeV14z7ALWS8owmsew=w1920-h969" width="72" height="99" alt=""/> 
        </div>
    </div>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modules that adjust with SMACSS techniques

In my experience with SMACSS, I have encountered a common issue - determining the correct approach to constructing adaptive content modules. For instance, let's say I have layout grid classes (.container, .row, .span-1, .span-N) along with media quer ...

How can data be effectively passed between templates in Angular?

I have two templates: in the first template, I am using a function and after its successful execution, I want to retrieve data in the second template. How can I achieve this? Both templates are utilizing the same controller. First Template: <form ng-s ...

Text buttons on the menu are running into each other when displayed on an Android device

After recently completing a redesign of my website, hetoft.com, I am proud to say that I crafted the design and code entirely by hand, with only Dreamweaver as a helpful tool. This was my first experience creating a website with a CSS-based layout, and des ...

Personalize the breakpoints of Bootstrap 4's grid system

In my project, I am facing a situation where I need a 1280 breakpoint to switch from desktop to tablet, instead of the xl breakpoint at 1200 as defined in Bootstrap. How can I globally change the xl breakpoint in Bootstrap without recompiling from the sour ...

Is there a way to modify link colors within a table utilizing CSS without the need for the "class" parameter or access to the HTML file?

I am currently working on giving a complete overhaul to an ancient website that I don't have access to its hosting server. The HTML is outdated from the 1990s, and there are many glaring errors in the code which make it difficult to fix without causin ...

Generate a fresh collection of objects all sharing a common identifier within a given array

Looking for help transforming this schedules array to match the desired output. Any ideas? let schedules = [ {day: 'Sunday', time: '5:00 PM'}, {day: 'Monday', time: '4:00 PM'}, {day: 'Monday', time: &ap ...

The tag's onclick function that was dynamically created was not triggering in jQuery

I created a web application using jquery mobile and ran into an issue. I am trying to call a function by clicking on a dynamically generated anchor tag, but it's not working and showing an error that the function is not defined. Any assistance on this ...

After deleting all the duplicates, the array is now empty

I've been working on removing duplicate entries from an array of objects, specifically targeting instances where the infoPageId appears more than once. Initially, everything was running smoothly with static data. However, after switching to calling m ...

tips for reducing the size of the recaptcha image to display just one word

The custom recaptcha theme I'm working with requires the image to be smaller in width but not in height. Is there a way to achieve this requested design? ...

Tips for integrating scroll-snap and jQuery's .scroll() function together

I'm facing challenges in integrating both of these elements into the same project. When I activate overflow: scroll, my jQuery function does not work as expected. However, if I deactivate it, then the jQuery works but the scroll-snap feature does not ...

Maximum Age Setting for Iron Session Cookie

Currently, I am attempting to configure a Next JS application with iron-session and a 'remember me' feature. The objective is for the maxAge of the iron-session cookie to be extended to a week if the user selects the remember me option on the log ...

Can you explain the significance of this error message that occurs when attempting to execute a node.js script connected to a MySQL database?

const mysql = require('mysql'); const inquirer = require('inquirer'); const connection = mysql.createConnection({ host: "localhost", port: 8889, user: "root", password: "root", database: "bamazon" }) connection.conn ...

What measures can I implement to prevent unauthorized access to my user login page?

Hey there! I'm currently working on an API project that involves setting up a login system using NodeJs and a mySQL database. Although I've successfully connected to the database and created a basic login page, I'm encountering an issue whe ...

Steps for invoking a function in the parent component from the child component

Within my main Class, I have a function named addFunc. This function calls the RenderItem function to display a list of items. Each item in this list has an onClick event that is supposed to trigger the addFunc function. The challenge I am facing is the i ...

Determine if a specific value is present in an array of objects using AngularJS

var arr = [ { id:1}, {id:2} ]; var obj = {id:1}; var result = arr.indexOf(obj.id) == -1; console.log(result); I am trying to determine if obj.id exists in the array arr[]. Please note: arr[] is an array of objects ...

Using AJAX, retrieve the attribute of a button and assign it to a node

I'm currently facing a dilemma in trying to pass the name of a clicked button to my node server. I have this code snippet in Node.js: app.get('/someUrl', function(req, res) { console.log(req.body.name); }); Meanwhile, my jQuery/ajax ...

Is there a way to change the color of a number's font based on whether it is preceded by a "+" or "-" sign?

I am currently working on a stocks widget and I have a specific requirement. Whenever there is a change in value in the Change or Changeinpercent columns, I need to dynamically set the font color to red for a decrease (-) or green for an increase (+). Her ...

jQuery functions are failing to execute when called within the .ready() function

I am new to using jQuery and I am facing an issue with setting up my click events. The strange thing is that when I use alert('');, it shows that the menu.js file is referenced correctly. However, when I try to use alert within the .ready() funct ...

Tips for customizing the time selector in material-ui-time-picker

Is there a way to enable keyboard input control for the material-ui-time-picker? Currently, it only allows editing when clicking on the clock interface. Here is a snippet of my code: import React, { Component } from "react"; import { TimePicker } from " ...

the router is having trouble choosing the right function

When attempting to log in a user using postman with the URL http://localhost:3000/login, it seems to always trigger the register function instead. The code itself is working fine, but it's just routing to the wrong function. How can I redirect it to t ...