This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Hugo des Gayets
Posted Jan 19 - Read on Facebook

Hi!
Could someone explain me how I could apply layer.animate to all layers stored in the array instead of the last one? Thanks!

8 Comments

Adria Jimenez

Hello Hugo des Gayets you can use @animate instead of layer.animate as "@" will contain the appropiate layer every time the click is fired. You can see an example of what I mean here: http://share.framerjs.com/c2xtp5i9elqy/

Ben Rodenhäuser

adding '(event,layer)' to the part of your code where you set up the event should also work, like here: http://share.framerjs.com/99e3kr43oe9i/

Hugo des Gayets

Thanks Adria Jimenez I'll try this!

Hugo des Gayets

Thanks Ben Rodenhäuser!

Jordan Robert Dobson

Basically by the time you click layer is equal to the last element in the loop because there aren't any more to set layer to. So you have to use "this" as in this.layer or @.layer.

This refers to the element clicked that calls that function.

Hugo des Gayets

Thanks Jordan Robert Dobson!

Marc Krenn

1. You can get rid of the allLayers.push(layer) by adding every layer to the array at their creation:

'layer = allLayers[i] = new Layer'

2. You can also git rid of the second loop (just remove line 26)! Instead of animating layer ('layer.animate') on Click, use '@.animate' or 'this.animate'.

Two examples:
1. http://share.framerjs.com/3woxywmi8t5o/
... here I'm using states instead of animation, but its the same logic

2. http://share.framerjs.com/peglr4t5j3q8/
... contains an in-depth description of how for-loops work in conjuncture with arrays :)

Lukas Imrich

Or you can make use of closures, which is probably the least preferable solution but it works as well:
http://share.framerjs.com/esc5j05yriq6/

Read the entire post on Facebook