0

I have this SVG element in HTML:

<g id="Group-8" transform="translate(108.800000, 0.293172)" fill="#FF056A" opacity="0.1">

Here you can see an attribute called opacity and it's value is 0.1

Now I want to increase it's value to 0.3, 0.6, 0.9 and then again to 0.6, 0.3, 0.1. It should do this all the time.

I have used Javascript setTimeout method but don't know how to make it do what I want :(

setTimeout(function() {
    $("#Group-8-Copy-2").attr('opacity', '0.3');
}, 1000);
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • 1
    `setTimeout` will only fire the function once. You should use `setInterval`if you want to repeat an action each second. With some global state you should be able to do what you want – Axnyff Sep 09 '18 at 13:47
  • @Axnyff should I use this setTimeout in a function? – Shibbir Sep 09 '18 at 13:51
  • Have a look at https://stackoverflow.com/questions/22154129/javascript-settimeout-loops . It's pretty much the same question as yours within a little bit different context. – Tomáš Pospíšek Sep 09 '18 at 13:57
  • @TomášPospíšek I see. – Shibbir Sep 09 '18 at 14:00

3 Answers3

1

Checkout below snippet:

$( document ).ready(function() {
    setInterval(function(){
      
      opacity = $('#tilak').css("opacity");
      opacity = Number(opacity) + 0.3;
      if (opacity > 1) {
        opacity = 0;
      }
      $('#tilak').css("opacity", opacity);
      console.log(opacity);
    }, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body id="tilak" style="opacity: 0">
kdjkd
</body>
Tilak Putta
  • 758
  • 4
  • 18
1

Here you go

var value = [0.1, 0.3, 0.6, 0.9];
var add = true;
var i = 0;

setInterval(function() {
  $("#Group-8-Copy-2").attr('opacity', [value[i]]);
  
  i = add ? i + 1 : i -1;
  add = i == value.length -1 ? false : (i == 0 ? true : add );
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <g id="Group-8-Copy-2" fill="#FF056A" opacity="0.1">
    <circle cx="10" cy="10" r="5" />
  </g>
</svg>
Useless Code
  • 12,123
  • 5
  • 35
  • 40
  • Wow, Great, but can it be more smooth ? – Shibbir Sep 09 '18 at 14:54
  • I would start `i` at `1` since the original SVG already sets the opacity to `0.1`, that way you aren't waiting an extra second to see it start animating. – Useless Code Sep 09 '18 at 15:34
  • @creativeartbd Adding more in-between steps to the `value` array would make the animation smoother, although you would have to adjust the interval to keep the animation the same length/speed. – Useless Code Sep 09 '18 at 15:37
1

You can also achieve a pulsing without JS, using CSS animations. Using CSS animations often has advantages over animating with JS, you can more precisely adjust the timing of things by changing the animation-duration and tweaking other things instead of adjusting the length of the timeout on a timer, etc. It also tends to perform better because it is the browser doing all the work rather than your code being interpreted by he browser and interacting with the DOM to make it work.

.stepped-pulse {
  animation-duration: 4s;
  animation-name: stepped-pulse;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: steps(4, end);
}

@keyframes stepped-pulse {
  0% {
    opacity: .01;
  }
  100% {
    opacity: .09;
  }
}
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <g id="Group-8-Copy-2" class="stepped-pulse" fill="#FF056A">
    <circle cx="10" cy="10" r="5" />
  </g>
</svg>

If you want a smoother animation (as you seem to indicate in comments on another answer), you can just change the animation-timing-function (in this case I am removing it ant it will default to ease):

.pulse {
  animation-duration: 4s;
  animation-name: pulse;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes pulse {
  0% {
    opacity: .01;
  }
  
  100% {
    opacity: .09;
  }
}
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <g id="Group-8-Copy-2" class="pulse" fill="#FF056A">
    <circle cx="10" cy="10" r="5" />
  </g>
</svg>
Useless Code
  • 12,123
  • 5
  • 35
  • 40