get()
返回目标属性的当前值,可选择单位转换或移除。
const value = utils.get(target, property, unit);
参数
| 名称 | 接受 | 描述 |
|---|---|---|
| target | Targets | 目标元素 |
| property | String |
目标的有效属性名称 |
| unit (opt) | String | Boolean |
如果设置为false则去掉单位,或者如果传入有效的单位String则转换单位 |
返回
返回的属性值类型或在满足条件时的以下类型:
| 类型 | 条件 |
|---|---|
String |
目标是一个 HTMLElement 或 SVGElement,并且 unit 参数未设置为 false 或设置为有效的单位 String |
Number |
目标是一个 HTMLElement 或 SVGElement,并且 unit 参数设置为 false |
get() 代码示例
import { animate, utils } from 'animejs';
const [ $raw, $rem, $num ] = utils.$('.value');
const [ $sq1, $sq2, $sq3 ] = utils.$('.square');
const getValues = () => {
// Return the raw parsed value (string with px)
$raw.textContent = utils.get($sq1, 'x');
// Return the converted value with unit (string with rem)
$rem.textContent = utils.get($sq2, 'x', 'rem');
// Return the raw value with its unit removed (number)
$num.textContent = utils.get($sq3, 'x', false);
}
animate('.square', {
x: 270,
loop: true,
alternate: true,
onUpdate: getValues
});
<div class="medium row">
<div class="square"></div>
<div class="padded label"><span class="raw value"></span></div>
</div>
<div class="medium row">
<div class="square"></div>
<div class="padded label"><span class="rem value"></span></div>
</div>
<div class="medium row">
<div class="square"></div>
<div class="padded label"><span class="num value"></span></div>
</div>