使用动态变量作为对象文字,jQuery动画功能 [英] Using a dynamic variable as object literal, jQuery animate function

查看:69
本文介绍了使用动态变量作为对象文字,jQuery动画功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我有

targetWater.animate({
    "width": "+=100%"

现在我想动态使用width或height

Now I want to dynamically use "width" or "height"

var direction = (targetWater.hasClass('x'))? "width" : "height";
targetWater.animate({
    direction: "+=100%"

但这不起作用。

我试过了

direction.toString()

''+direction+''

没有喜悦这个

var anim = { direction: "+=100%" }
targetWater.animate(anim,


推荐答案

您的方法不起作用,因为 direction 被解释为一个键,而不是一个变量。

Your approach doesn't work since direction is interpreted as a key, not a variable.

你可以这样做:

var animation = {};
var direction = targetWater.hasClass('x') ? "width" : "height"
animation[direction] = "+=100%";
targetWater.animate(animation);

方括号使得可以动态地拥有密钥。

The square brackets make it so you can have the key dynamically.

如果你想要方块方向你要写的括号表示法:

If you would want the key "direction" with the square bracket notation you would write:

animation["direction"];

相当于:

animation.direction;

这篇关于使用动态变量作为对象文字,jQuery动画功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆