I have an svg element. How can i show it gradually (like an animation) from left to right?
Asked
Active
Viewed 819 times
2 Answers
2
You can use CSS @keyframes.
@keyframes stretchInFromLeft {
0% {
width: 100%;
}
100% {
width: 0;
}
}
img {
max-width: 100%;
width: 100%;
}
.wrapper {
position: relative;
}
.overlay {
animation: 1s ease-out 0s 1 stretchInFromLeft;
position: absolute;
top: 0;
right: 0;
background: #fff;
width: 0;
height: 100%;
}
<div class="wrapper">
<img src="https://i.stack.imgur.com/dta2g.jpg" />
<div class="overlay"></div>
</div>
Fiddle: https://jsfiddle.net/r034wcgp/1/

Pavol Velky
- 770
- 3
- 9
-
ok. i have another question. how can i start animation from start? it starts not from the start point – Mario Kit Dec 22 '20 at 23:41
1
You could animate a SVG clipPath with jQuery animate() like this:
$("#cut-off-bottom rect").animate({width: "100%", duration:4000})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320">
<clipPath id="cut-off-bottom">
<rect x="0" y="0" width="0" height="320" />
</clipPath>
<path fill="#0099ff" fill-opacity="1" clip-path="url(#cut-off-bottom)" d="M0,32L48,32C96,32,192,32,288,58.7C384,85,480,139,576,154.7C672,171,768,149,864,165.3C960,181,1056,235,1152,240C1248,245,1344,203,1392,181.3L1440,160L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
</svg>

Louys Patrice Bessette
- 33,375
- 6
- 36
- 64