简体   繁体   中英

how can i set left undefined in jquery animate

I've a array like

[undefined,"3px","3px",undefined,"3px",undefined,"3px",undefined]

it is marked direction like css;

so I want use

$(dom).animate{
        top:arr[0],
        right:arr[1],
        bottom:arr[2],
        left:arr[3],
}

but it always set top:0 right0 ,although it has seted undefined; thanks


thank you ;
i am sorry to expression not correct;
i've solve  it in other ways.
Simply say it's when i've set the dom left ;
than use animate to set the right property ;
it's not work because css property can't set left and right in time;
i'hava a lot of dom.Some set left and some set right ;
Some animate right and some animate left;
i make an array 4 direction one dom;
so i want to ask there is any way to remove css property in animate;

You cannot set css value as undefined . You can convert the value to 0 when your array value is undefined instead:

$(dom).animate{
    top: arr[0] == "undefined" ? 0 : arr[0],
    right: arr[1] == "undefined" ? 0 : arr[1],
    bottom: arr[2] == "undefined" ? 0 : arr[2],
    left: arr[3] == "undefined" ? 0 : arr[3],
}

I don't think that the css understands an undefined value. Try switching out undefined with 0 or 0px instead.

Check datatype of variable using typeof()

 $(dom).animate{
   top: typeof(arr[0]) == "undefined" ? 0 : arr[0],
   right: typeof(arr[1])  == "undefined" ? 0 : arr[1],
   bottom: typeof(arr[2])  == "undefined" ? 0 : arr[2],
  left: typeof(arr[3])  == "undefined" ? 0 : arr[3],
 }

You can map undefined to "+=0px" , so the animation becomes a no-op for undefined components:

var components = [
    undefined, "3px", "3px", undefined, "3px", undefined, "3px", undefined
];
var animComponents = $.map(components, function(value) {
    return value === undefined ? "+=0px" : value;
});

$(dom).animate({
    top: animComponents[0],
    right: animComponents[1],
    bottom: animComponents[2],
    left: animComponents[3]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM