跳过主要内容

distribute

返回值:函数

返回一个函数,该函数根据您提供的输入来分配一个值数组。


根据各种配置选项将一个总量分配到数组中的各个元素。在内部,高级 stagger 效果就是这样使用的,但您可以将其应用于任何值。它基本上是根据数组中(或者网格中)元素的位置来分配值:

// get a function that, when fed an index value, will return a value according to the configuration options
let distributor = gsap.utils.distribute({
// the base value to start from (default:0)
base: 50,

// total amount to distribute across the targets (this amount gets added to the "base" when returned)
amount: 100,

// position in the targets array to begin from (can be an index number, a keyword like "start", "center",
// "edges", "random", or "end", or an array of ratios along the x-axis and y-axis like [0.25, 0.75]) (default: 0)
from: "center",

// bases distribution on the element's position in a grid [rows, columns] instead of a flat array.
// You can also define the rows and columns in array format like [5, 10]
grid: "auto",

// for grid-based distributing, you can limit measurements to one axis ("x" or "y")
axis: "y",

// distributes based on an ease curve!
ease: "power1.inOut",
});

// get an array of all the elements with the class ".box" applied
let targets = gsap.utils.toArray(".box");

// Now for any target element, we can just feed in its index from the targets array (along with the target
// and array) and it'll do all the calculations and return the appropriate amount:
let distributedValue = distributor(2, targets[2], targets);

这可以直接在补间动画中使用:

// animate the scale of all ".class" elements so that the ones in the middle are 0.5 and the ones on
// the outer edges are 3
gsap.to(".class", {
scale: gsap.utils.distribute({
base: 0.5,
amount: 2.5,
from: "center",
}),
});

参数

  1. config : 对象- 用于声明如何分配输入的配置对象。此对象中的所有属性都是可选的。可以使用这些属性:
    • base : 数字- 起始的基础值。默认值为 0。
    • amount : 数字- 要分配给所有目标的总数量(返回时会将这个数量加到 "base" 上)。因此如果amount设置为1并且有 100 个目标,则每个返回值之间会有 0.01 的差异。如果您想指定每个目标之间的特定数量,请改用each属性instead.
    • 这些元素创建 ScrollTrigger,并且: 数字 - 每个目标之间要添加的数量(返回时会将这个数量加到 "base" 上)。因此如果each设置为1并且有 4 个目标,它将返回 0、1、2 和 3。如果您更希望指定一个total总量在目标之间进行分割,请改用amount属性instead.
    • 来自: [数字 | 字符串 | 数组] - 从目标数组中的哪个位置开始(可以是一个索引号,像"start", "center", "edges", "random",或者"end"这样的关键字,或像[0.25, 0.75]这样表示 x 轴和 y 轴比例的数组)。默认值是0.
    • grid: [字符串 | 数组] - 基于元素在网格 [行数, 列数] 中的位置进行分配,例如[5, 10],而不是一个平面数组。您可以使用"auto"让 GSAP 自动尝试检测 DOM 元素的列数和行数。
    • axis : 字符串- 对基于网格的分配,您可以限制只在一个轴上测量 ("x"或者"y")。
    • ease : 缓动- 按 ease 曲线进行分布!默认值是"none".

SnorklTV 的GSAP 3: Beyond the Basics 课程中关于 distribute 的这段视频也许有助于您的理解。

以及视频中使用的配套代码示例。

加载中...

无噪 Logo
无噪文档
中文文档 · 复刻官网
查看所有 ↗