Make SVG graphics play on entering window (scroll)

Viewed 46

I have a tough question! I have several SVG graphics on some of my pages which play when the page loads. But because many of them are below the fold, I would like them to start playing (and play once) when they scroll into view.

NOTE: I have searched everywhere for a solution and even hired a freelancer who couldn't make it work. The difference with my SVGs I think is that they use @keyframe and have more than one path that play in sequence which may be why other solutions do not work? The reason for more than one path is that it is a calligraphic arrow shape that overlaps when revealed by the SVG mask.

My HTML for one of the SVGs (inside a DIV to make it responsive):

<div id="arrow-1id" class="arrow-1 leftarrows">
  <!--START ARROW 1-->
  <svg id="arrow1svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 436.55 256.959">
                        
                        <mask id="arrow1-mask1" maskUnits="userSpaceOnUse">
                        <path d="M4.537,54.288
                            c96.333-80.667,213.667,135.333,90.667,96.333"/>
                        </mask>
                        
                        <mask id="arrow1-mask2" maskUnits="userSpaceOnUse">
                        <path d="M100.87,156.955
                            c-29.667-100.667,29.666-152,78.333-146.667s151.667,255.667,29.667,234"/>
                        </mask>
                        
                        <mask id="arrow1-mask3" maskUnits="userSpaceOnUse">
                        <path d="M214.87,248.956
                            c-53.667-43.667,15.334-337.334,195.667-169.667"/>
                        </mask>
                        
                        <mask id="arrow1-mask4" maskUnits="userSpaceOnUse">
                        <path d="M322.203,53.955c108.667,43,117,69.09,68-53.955"/>
                        </mask>
                        
                            <path mask="url(#arrow1-mask1)" fill="#42A8FC" d="M98.168,50.639C66.146,29.84,27.535,21.405,0.142,44.781c-1.983,1.692,17.501,16.275,21.915,12.509
                            c21.17-18.066,49.736-15.119,72.854-0.936c11.126,6.518,19.094,15.658,19.094,15.658c8.426,9.078,14.961,19.84,18.385,31.416
                            c4.701,15.891,0.705,30.535-10.91,41.153c-0.855,0.782-1.803,1.409-2.783,1.981c-0.01,0.004-6.326,4.56-16.162,2.465l-6.889,7.466
                            c9.913,8.681,22.827,13.119,36.498,7.999c43.123-16.156,23.759-65.003-14.488-98.967C117.654,65.526,108.286,57.122,98.168,50.639z"/>
                            
                            <path mask="url(#arrow1-mask2)" fill="#42A8FC" d="M231.959,50.614c-17.516-25.197-43.061-49.605-73.761-50.592c-20.618-0.664-34.757,13.81-44.931,27.774
                            c-5.189,7.124-10.349,14.789-15.1,22.843l-3.258,5.715C84.467,75.328,76.75,96.273,76.766,117.7
                            c0.01,13.138,7.45,28.785,18.879,38.794l6.889-7.466c-0.658-1.355-1.329-2.721-1.771-4.061
                            c-7.573-22.907,0.716-49.699,13.241-72.955l3.65-6.486c7.376-12.557,15.713-23.818,23.057-32.426
                            c8.87-10.398,23.658-23.112,40.648-19.129c14.01,3.285,24.33,19.391,31.584,30.21c3.548,5.288,7.105,11.241,10.578,17.655
                            l12.133,25.446c22.227,53.15,32.844,122.106-8.789,151.369c-3.873,2.722-8.496,4.636-12.935,6.029l-3.878,7.245
                            c25.657,15.113,52.242-6.228,60.439-31.938c14.138-44.338-1.676-101.902-24.266-145.678
                            C246.227,74.311,238.632,60.523,231.959,50.614z"/>
                            
                            <path mask="url(#arrow1-mask3)" fill="#42A8FC" d="M406.734,68.147c-17.148-17.841-37.951-33.412-59.953-44.124c-50.594-24.632-88.095-5.991-114.822,26.591
                            l-8.438,11.223c-3.395,4.914-6.599,10.027-9.592,15.281c-17.29,30.335-29.316,64.641-31.375,100.486
                            c-1.105,19.22-1.053,44.238,13.145,61.586c4.664,5.7,9.492,9.873,14.354,12.736l3.878-7.245c-3.323-4.796-5.046-11.189-6.378-16.149
                            c-5.317-19.783-3.871-40.435-0.658-59.469c4.553-26.978,14.076-56.253,28.76-81.78l9.521-14.934
                            c8.408-11.894,18.098-22.568,29.177-31.183c41.766-32.474,90.141-8.049,122.342,30.429
                            C404.036,81.622,413.766,75.922,406.734,68.147z"/>
                            
                            <path mask="url(#arrow1-mask4)" fill="#42A8FC" d="M404.413,71.956c0,0-3.166,0-12.344-3.026c-17.545-5.735-35.092-11.471-52.635-17.206
                            c-15.207-4.971-15.507,16.587-1.976,21.011c27.503,8.99,55.004,17.981,82.506,26.972c9.534,3.115,13.706-5.336,11.287-12.662
                            c-8.52-25.797-17.039-51.594-25.558-77.392c-4.546-13.767-25.338-10.047-20.597,4.315c5.671,17.17,11.341,34.342,17.013,51.514
                            L404.413,71.956z"/>
                        
                        </svg>

  <style>
    #arrow1-mask1 path {
      fill: none;
      stroke: white;
      stroke-width: 39;
      stroke-dasharray: 265.809 265.809;
      stroke-dashoffset: 0;
      animation: brush1a 2s linear;
      animation-fill-mode: forwards;
    }
    
    #arrow1-mask2 path {
      fill: none;
      stroke: white;
      stroke-width: 39;
      stroke-dasharray: 499.444 499.444;
      stroke-dashoffset: 0;
      animation: brush1b 2s linear;
      animation-fill-mode: forwards;
    }
    
    #arrow1-mask3 path {
      fill: none;
      stroke: white;
      stroke-width: 39;
      stroke-dasharray: 396.144 396.144;
      stroke-dashoffset: 0;
      animation: brush1c 2s linear;
      animation-fill-mode: forwards;
    }
    
    #arrow1-mask4 path {
      fill: none;
      stroke: white;
      stroke-width: 39;
      stroke-dasharray: 195.971 195.971;
      stroke-dashoffset: 0;
      animation: brush1d 2s linear;
      animation-fill-mode: forwards;
    }
    
    @keyframes brush1a {
      0% {
        stroke-dashoffset: 265.809;
      }
      1% {
        stroke-dashoffset: 265.809;
      }
      20% {
        stroke-dashoffset: 0;
      }
      100% {
        stroke-dashoffset: 0;
      }
    }
    
    @keyframes brush1b {
      0% {
        stroke-dashoffset: 499.444;
      }
      20% {
        stroke-dashoffset: 499.444;
      }
      60% {
        stroke-dashoffset: 0;
      }
      100% {
        stroke-dashoffset: 0;
      }
    }
    
    @keyframes brush1c {
      0% {
        stroke-dashoffset: 396.144;
      }
      60% {
        stroke-dashoffset: 396.144;
      }
      80% {
        stroke-dashoffset: 0;
      }
      100% {
        stroke-dashoffset: 0;
      }
    }
    
    @keyframes brush1d {
      0% {
        stroke-dashoffset: 195.971;
      }
      80% {
        stroke-dashoffset: 195.971;
      }
      100% {
        stroke-dashoffset: 0;
      }
    }
  </style>
  <!--END ARROW 1-->
</div>

I haven't posted any JS because none of the code I have tried works.

2 Answers

You can achieve that by running the animation if the container #arrow-1id as a specific class (lets say .play). Then you just check with javascript when the container appear in the screen. When it's the case, just add the .play class to the container:

var body = document.getElementsByTagName("body")[0];
var div = document.getElementById("arrow-1id");

body.onscroll = function() {
  if (document.documentElement.scrollTop >= div.offsetTop - 200) {
    div.classList.add("play");
  }
};
body {
  height: 2000px;
}

#arrow-1id {
  margin-top: 400px;
  background-color: lightgrey;
}

.play #arrow1-mask1 path {
  fill: none;
  stroke: white;
  stroke-width: 39;
  stroke-dasharray: 265.809 265.809;
  stroke-dashoffset: 0;
  animation: brush1a 2s linear;
  animation-fill-mode: forwards;
}

.play #arrow1-mask2 path {
  fill: none;
  stroke: white;
  stroke-width: 39;
  stroke-dasharray: 499.444 499.444;
  stroke-dashoffset: 0;
  animation: brush1b 2s linear;
  animation-fill-mode: forwards;
}

.play #arrow1-mask3 path {
  fill: none;
  stroke: white;
  stroke-width: 39;
  stroke-dasharray: 396.144 396.144;
  stroke-dashoffset: 0;
  animation: brush1c 2s linear;
  animation-fill-mode: forwards;
}

.play #arrow1-mask4 path {
  fill: none;
  stroke: white;
  stroke-width: 39;
  stroke-dasharray: 195.971 195.971;
  stroke-dashoffset: 0;
  animation: brush1d 2s linear;
  animation-fill-mode: forwards;
}

@keyframes brush1a {
  0% {
    stroke-dashoffset: 265.809;
  }
  1% {
    stroke-dashoffset: 265.809;
  }
  20% {
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dashoffset: 0;
  }
}

@keyframes brush1b {
  0% {
    stroke-dashoffset: 499.444;
  }
  20% {
    stroke-dashoffset: 499.444;
  }
  60% {
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dashoffset: 0;
  }
}

@keyframes brush1c {
  0% {
    stroke-dashoffset: 396.144;
  }
  60% {
    stroke-dashoffset: 396.144;
  }
  80% {
    stroke-dashoffset: 0;
  }
  100% {
    stroke-dashoffset: 0;
  }
}

@keyframes brush1d {
  0% {
    stroke-dashoffset: 195.971;
  }
  80% {
    stroke-dashoffset: 195.971;
  }
  100% {
    stroke-dashoffset: 0;
  }
}
<div id="arrow-1id" class="arrow-1 leftarrows">
  <!--START ARROW 1-->
  <svg id="arrow1svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 436.55 256.959">

    <mask id="arrow1-mask1" maskUnits="userSpaceOnUse">
      <path d="M4.537,54.288
                            c96.333-80.667,213.667,135.333,90.667,96.333" />
    </mask>

    <mask id="arrow1-mask2" maskUnits="userSpaceOnUse">
      <path d="M100.87,156.955
                            c-29.667-100.667,29.666-152,78.333-146.667s151.667,255.667,29.667,234" />
    </mask>

    <mask id="arrow1-mask3" maskUnits="userSpaceOnUse">
      <path d="M214.87,248.956
                            c-53.667-43.667,15.334-337.334,195.667-169.667" />
    </mask>

    <mask id="arrow1-mask4" maskUnits="userSpaceOnUse">
      <path d="M322.203,53.955c108.667,43,117,69.09,68-53.955" />
    </mask>

    <path mask="url(#arrow1-mask1)" fill="#42A8FC" d="M98.168,50.639C66.146,29.84,27.535,21.405,0.142,44.781c-1.983,1.692,17.501,16.275,21.915,12.509
                            c21.17-18.066,49.736-15.119,72.854-0.936c11.126,6.518,19.094,15.658,19.094,15.658c8.426,9.078,14.961,19.84,18.385,31.416
                            c4.701,15.891,0.705,30.535-10.91,41.153c-0.855,0.782-1.803,1.409-2.783,1.981c-0.01,0.004-6.326,4.56-16.162,2.465l-6.889,7.466
                            c9.913,8.681,22.827,13.119,36.498,7.999c43.123-16.156,23.759-65.003-14.488-98.967C117.654,65.526,108.286,57.122,98.168,50.639z" />

    <path mask="url(#arrow1-mask2)" fill="#42A8FC" d="M231.959,50.614c-17.516-25.197-43.061-49.605-73.761-50.592c-20.618-0.664-34.757,13.81-44.931,27.774
                            c-5.189,7.124-10.349,14.789-15.1,22.843l-3.258,5.715C84.467,75.328,76.75,96.273,76.766,117.7
                            c0.01,13.138,7.45,28.785,18.879,38.794l6.889-7.466c-0.658-1.355-1.329-2.721-1.771-4.061
                            c-7.573-22.907,0.716-49.699,13.241-72.955l3.65-6.486c7.376-12.557,15.713-23.818,23.057-32.426
                            c8.87-10.398,23.658-23.112,40.648-19.129c14.01,3.285,24.33,19.391,31.584,30.21c3.548,5.288,7.105,11.241,10.578,17.655
                            l12.133,25.446c22.227,53.15,32.844,122.106-8.789,151.369c-3.873,2.722-8.496,4.636-12.935,6.029l-3.878,7.245
                            c25.657,15.113,52.242-6.228,60.439-31.938c14.138-44.338-1.676-101.902-24.266-145.678
                            C246.227,74.311,238.632,60.523,231.959,50.614z" />

    <path mask="url(#arrow1-mask3)" fill="#42A8FC" d="M406.734,68.147c-17.148-17.841-37.951-33.412-59.953-44.124c-50.594-24.632-88.095-5.991-114.822,26.591
                            l-8.438,11.223c-3.395,4.914-6.599,10.027-9.592,15.281c-17.29,30.335-29.316,64.641-31.375,100.486
                            c-1.105,19.22-1.053,44.238,13.145,61.586c4.664,5.7,9.492,9.873,14.354,12.736l3.878-7.245c-3.323-4.796-5.046-11.189-6.378-16.149
                            c-5.317-19.783-3.871-40.435-0.658-59.469c4.553-26.978,14.076-56.253,28.76-81.78l9.521-14.934
                            c8.408-11.894,18.098-22.568,29.177-31.183c41.766-32.474,90.141-8.049,122.342,30.429
                            C404.036,81.622,413.766,75.922,406.734,68.147z" />

    <path mask="url(#arrow1-mask4)" fill="#42A8FC" d="M404.413,71.956c0,0-3.166,0-12.344-3.026c-17.545-5.735-35.092-11.471-52.635-17.206
                            c-15.207-4.971-15.507,16.587-1.976,21.011c27.503,8.99,55.004,17.981,82.506,26.972c9.534,3.115,13.706-5.336,11.287-12.662
                            c-8.52-25.797-17.039-51.594-25.558-77.392c-4.546-13.767-25.338-10.047-20.597,4.315c5.671,17.17,11.341,34.342,17.013,51.514
                            L404.413,71.956z" />

  </svg>


  <!--END ARROW 1-->
</div>

I tried this and it works but it seems only supported in Firefox, no other browser???

<script type="text/javascript">
    
                var diva2 = document.getElementById("arrow-2id");
                
                window.addEventListener("scroll", function() {
                  if (document.documentElement.scrollTop >= diva2.offsetTop - -500) {
                    diva2.classList.add("play");
                  }
                  
                 }
                );
                </script>
Related