creating a both side radio wave animation

后端 未结 2 1509
醉酒成梦
醉酒成梦 2021-01-27 17:04

I want a Radar wave animation like this:

With a lot of effort using my poor knowledge of Svgs I added the left side of the desired wave and it works fine. but I n

2条回答
  •  清歌不尽
    2021-01-27 17:28

    Accessing elements wave1..wave6 should be like querySelectorAll:

    const wave1 = document.querySelectorAll(".radio-wave-1");
    ...
    const wave6 = document.querySelectorAll(".radio-wave-6");
    

    Because this is a collection.

    Next, I applied a for loop, specifying 2 as index to add a class for each wave1..wave6.

    Also, I decided to correct your html and css layout. If you want your waves to be in the picture that you showed, then you need to do the following:

    Use flex to align with each other. Add this to your css:

    .wave-container {
        display: flex;
        justify-content: center;
    }
    

    And remove rule margin-right: 50% from .left-wave-container and .right-wave-container selectors.

    Now it's done!

    function Wave() {
      const waves = document.querySelectorAll(".radio-wave");
      const wave1 = document.querySelectorAll(".radio-wave-1");
      const wave2 = document.querySelectorAll(".radio-wave-2");
      const wave3 = document.querySelectorAll(".radio-wave-3");
      const wave4 = document.querySelectorAll(".radio-wave-4");
      const wave5 = document.querySelectorAll(".radio-wave-5");
      const wave6 = document.querySelectorAll(".radio-wave-6");
    
      waves.forEach(function(wave) {
        wave.classList.add('add-wave');
    
        for (var i = 0; i < 2; i++) {    
          wave1[i].classList.add('add-radio-wave-1'); 
          wave2[i].classList.add('add-radio-wave-2');  
          wave3[i].classList.add('add-radio-wave-3'); 
          wave4[i].classList.add('add-radio-wave-4'); 
          wave5[i].classList.add('add-radio-wave-5'); 
          wave6[i].classList.add('add-radio-wave-6');  
        }  
      });
    }
    
    setTimeout(() => Wave(), 2000);
    body {
      background: black;
    }
    
    .wave-container {
        display: flex;
        justify-content: center;
    }
    
    .left-wave-container {
      position: relative;
      /*margin-right: 50%;*/
      padding: 0;
      font-size: 16px;
    }
    
    .right-wave-container {
      position: relative;
      /*margin-right: 50%;*/
      padding: 0;
      font-size: 16px;
      transform: rotate(180deg); /* flip the animation*/
    }
    
    .radio-wave-container {
      display: flex;
      flex-flow: column nowrap;
      justify-content: center;
      align-items: center;
      margin: 5rem;
    }
    
    .radio-wave-container.content {
      flex-flow: row nowrap;
      width: 100%;
    }
    
    .radio-source {
      position: absolute;
      right: 2rem;
      width: 2rem;
      fill: #fcba03;
    }
    
    .add-wave {
      -webkit-animation-duration: 2.2s;
              animation-duration: 2.2s;
      -webkit-animation-iteration-count: infinite;
              animation-iteration-count: infinite;
      -webkit-animation-timing-function: linear;
              animation-timing-function: linear;
    }
    
    .radio-wave {
      position: absolute;
      right: 2rem;
      stroke-linecap: round;
      stroke-width: 4;
      stroke: #fcba03;
      fill: none;
      transform: scale(0.2) translateX(150%);
      opacity: 0;
      width: 5rem;
      height: 5rem;
    }
    
    .add-radio-wave-1 {
      -webkit-animation-name: wave-1;
              animation-name: wave-1;
      -webkit-animation-delay: 0s;
              animation-delay: 0s;
    }
    
    @-webkit-keyframes wave-1 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-1 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    .add-radio-wave-2 {
      -webkit-animation-name: wave-2;
              animation-name: wave-2;
      -webkit-animation-delay: 0.2s;
              animation-delay: 0.2s;
    }
    
    @-webkit-keyframes wave-2 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-2 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    .add-radio-wave-3 {
      -webkit-animation-name: wave-3;
              animation-name: wave-3;
      -webkit-animation-delay: 0.4s;
              animation-delay: 0.4s;
    }
    
    @-webkit-keyframes wave-3 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-3 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    .add-radio-wave-4 {
      -webkit-animation-name: wave-4;
              animation-name: wave-4;
      -webkit-animation-delay: 0.6s;
              animation-delay: 0.6s;
    }
    
    @-webkit-keyframes wave-4 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-4 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    .add-radio-wave-5 {
      -webkit-animation-name: wave-5;
              animation-name: wave-5;
      -webkit-animation-delay: 0.8s;
              animation-delay: 0.8s;
    }
    
    @-webkit-keyframes wave-5 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-5 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    .add-radio-wave-6 {
      -webkit-animation-name: wave-6;
              animation-name: wave-6;
      -webkit-animation-delay: 1s;
              animation-delay: 1s;
    }
    
    @-webkit-keyframes wave-6 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    @keyframes wave-6 {
      0% {
        transform: scale(0.2) translateX(150%);
        opacity: 0.2;
      }
      10% {
        transform: scale(0.3) translateX(50%);
        opacity: 0.3;
      }
      20% {
        transform: scale(0.4) translateX(-40%);
        opacity: 0.4;
      }
      30% {
        transform: scale(0.5) translateX(-120%);
        opacity: 0.5;
      }
      40% {
        transform: scale(0.6) translateX(-190%);
        opacity: 0.6;
      }
      50% {
        transform: scale(0.7) translateX(-250%);
        opacity: 0.7;
      }
      60% {
        transform: scale(0.8) translateX(-300%);
        opacity: 0.8;
      }
      70% {
        transform: scale(0.9) translateX(-350%);
        opacity: 0.9;
      }
      80% {
        transform: scale(1) translateX(-400%);
      }
      90% {
        transform: scale(1.1) translateX(-450%);
      }
      100% {
        transform: scale(1.2) translateX(-500%);
      }
    }
    
    
    
    
        
         
    
    
     
    
        

提交回复
热议问题