Home  >  Article  >  Web Front-end  >  How to create a shooting star animation effect using CSS?

How to create a shooting star animation effect using CSS?

WBOY
WBOYforward
2023-09-05 09:33:221180browse

How to create a shooting star animation effect using CSS?

Meteors appear to be the twinkling signs of warmth these little shivers produce as they twinkle across the cold night sky. The shooting star effect is one of the most unique background effects for dark theme websites. The shooting star animation is a great example of a loading screen that can hold your attention for a long time while the rest of the material on the website loads. This effect can be exploited in page loaders and user interfaces.

In this article, we will discuss ways to create a meteor animation effect using CSS. For this we will use various properties of CSS like animation, overflow, filter, transform, nth child property, :before and :after pseudo-selectors.

Steps to follow

The following are the steps to create a shooting star animation effect -

Step 1 - Create the basic design of the star using HTML. Create one partial element for the sky and nine

elements for the stars.

Step 2 - To make the star fall at 45 degrees, use the Transform property of the Section element.

Step 3 - Align the p elements according to your requirements.

Step 4 - Make a donut-shaped ball using properties such as position and fill properties. To give them a circular shape, use the border-radius property.

Step 5 - Use :before and :after pseudo-selectors to specify before and after for the stars Effect

Step 6 - Use the nth sub-property to add animation effects to the stars. Specifies the position of each nth child.

Step 7 - Using @keyframes, specify the width of the star's head and tail. Use @-webkit-keyframes to create shooting effects.

Attributes used

We used the following CSS properties -

:nth-child(n) selector

:nth-child(n) is a CSS pseudo-class selector used to match elements based on their position within a sibling group. It matches all elements of the nth child element. n can be a number, keyword, or any formula.

grammar

element :nth-child(n){
   Css declarations;
}

The parameter "n" in brackets indicates the pattern of selecting or matching elements. It may be an even or odd function sign.

Odd values ​​represent items with odd positions in the series, such as 1, 3, 5, etc. Likewise, even values ​​represent items with an even number in the series, such as 2, 4, 6, and so on.

CSS animation

The animation property of CSS allows us to change various style properties of an element within a certain time interval, thereby giving it an animation effect.

@keyframes Used to specify exactly what happens in the animation for a given duration. This is done by declaring CSS properties for certain "frames" during the animation, with percentages ranging from 0% (start of animation) to 100% (end of animation).

Filter properties

It enables developers to add visual effects such as opacity, blur, and saturation to HTML elements.

grammar

filter: none | blur()| drop-shadow() | invert() | opacity() | saturate() | sepia() | url() | brightness()| contrast();

Background - It allows us to add visual effects to HTML elements in the background.

Box-shadow - It enables us to add shadow to HTML elements.

Transform - This property allows us to add a 2D or 3D transform to the element. It allows you to transform, rotate, scale, move, skew, etc. elements.

Example

<!DOCTYPE html>
<html>
<head>
   <title> Shooting Star Animation Effect </title>
   <style>
      *{
         margin: 0;
         padding: 0;
         box-sizing: border-box;
      }
      body{
         overflow: hidden;
      }
      div{
         position: absolute;
         top: 0;
         left: 0;
         background: #000;
         background-position-x: center;
         background-size: cover;
         width: 100%;
         height: 100vh;
         animation: background 68s linear infinite;
      }
      @keyframes background {
         0%{ transform:scale(1);}
         55%{ transform:scale(1.3);}
         100%{ transform: scale(1);}
      }
      span{
         position: absolute;
         left: 50%;
         top: 45%;
         width: 5px;
         height: 5px;
         background: white;
         border-radius: 50%;
         box-shadow: 0 1px 0 5px rgba(254, 254, 255, 0.2), 0 1px 0 7px rgba(245, 254, 255, 0.1), 0 1px 21px rgba(253, 253, 245, 1) ;
         animation: anim 3s ease-in-out infinite;
      }
      span::before{
         content: '';
         width: 290px;
         height: 2px;
         position: absolute;
         top: 53%;
         transform: translateY(-45%);
         background: linear-gradient(90deg, rgba(255, 255, 255, 1), transparent);
      }
      @keyframes anim {
         0%{ transform: rotate(325deg) translateX(0); opacity: 1; }
         40%{ opacity: 0.8; }
         70%{ opacity: 1; }
         100%{ transform: rotate(325deg) translateX(-1400px); opacity: 0; }
      }
      span:nth-child(1){
         top: 0;
         right: 0;
         left: inherit;
         animation-delay: 0 ;
         animation-duration: 1s;
      }
      span:nth-child(2){
         top: 0;
         right: 70px;
         left: inherit;
         animation-delay: 0.3s;
         animation-duration: 4s;
      }
      span:nth-child(3){
         top: 70px;
         right: 0px;
         left: inherit;
         animation-delay: 0.3s ;
         animation-duration: 3s;
      }
      span:nth-child(4){
         top: 0;
         right: 170px;
         left: initial;
         animation-delay: 0.7s;
         animation-duration: 3s;
      }
   </style>
</head>
<body>
   <div>
      <span> </span>
      <span> </span>
      <span> </span>
      <span> </span>
      <span> </span>
      <span> </span>
   </div>
</body>
</html>

in conclusion

In this article, we learned how to create a shooting star effect using CSS. Since web design has improved significantly, online animation is an important tool that website builders use to attract a larger audience. Most people try to use it more often, not just to fill the page but to demonstrate how the page should be read. Animations are used to show form errors, click locations, increase conversion rates, and more.

Animations often grab the user's attention, which is why they are used. Additionally, animations can be used to divert users’ attention while content is loading, giving the impression that it is loading faster and allowing them to immediately observe movement or progress.

The above is the detailed content of How to create a shooting star animation effect using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete