Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...

GreenSock Docs

distribute()

Returns : Function

Returns a function to distribute an array of values based on the inputs that you give it.


Distributes an amount across the elements in an array according to various configuration options. Internally, it’s what advanced staggers use, but you can apply it for any value. It essentially assigns values based on the element’s position in the array (or in a grid):

  1. //get a function that, when fed an index value, will return a value according to the configuration options
  2. var distributor = gsap.utils.distribute({
  3. //the base value to start from (default:0)
  4. base: 50,
  5. //total amount to distribute across the targets (this amount gets added to the "base" when returned)
  6. amount: 100,
  7. //position in the targets array to begin from (can be an index number, a keyword like "start", "center",
  8. //or "end", or an array of ratios along the x-axis and y-axis like [0.25, 0.75] (default: 0)
  9. from: "center",
  10. //bases distribution on the element's position in a grid [columns, rows] instead of a flat array.
  11. //You can also define the columns and rows in array format like [10, 5]
  12. grid: "auto",
  13. //for grid-based distributing, you can limit measurements to one axis ("x" or "y")
  14. axis: "y",
  15. //distributes based on an ease curve!
  16. ease: "Power1.easeInOut"
  17. });
  18. //get an array of all the elements with the class ".box" applied
  19. var targets = gsap.utils.toArray(".box");
  20. //now for any target element, we can just feed in its index from the targets array (along with the target
  21. //and array) and it'll do all the calculations and return the appropriate amount:
  22. var distributedValue = distributor(2, targets[2], targets);

This can be used directly in a tween:

  1. //animate the scale of all ".class" elements so that the ones in the middle are 0.5 and the ones on
  2. //the outer edges are 3
  3. gsap.to(".class", {
  4. scale: gsap.utils.distribute({
  5. base: 0.5,
  6. amount: 2.5,
  7. from: "center"
  8. })
  9. });
Copyright 2017, GreenSock. All rights reserved. This work is subject to theterms of useor for Club GreenSock members, the software agreement that was issued with the membership.
×