假设我有以下功能
function slide(element, direction, hide){
if (direction == "left") {
$(element).animate({
"left": 500,
}, 500, function(){
if(hide) {
$(element).css("display", "none");
}
});
} else {
$(element).animate({
"right": 500,
}, 500, function(){
if(hide) {
$(element).css("display", "none");
}
});
}
}
它本身就很有效。但我想消除 If else 术语来得到这样的东西
function slide(element, direction, hide){
$(element).animate({
direction : 500,
}, 500, function(){
if(hide) {
$(element).css("display", "none");
}
});
}
这根本不起作用...有没有办法通过动画函数中的变量控制左/右?
最佳答案
自己创建一个带有方向的对象,然后在动画函数中使用它,如下所示。
function slide(element, direction, hide) {
var obj = {};
obj[direction] = 500;
$(element).animate(obj, 500, function () {
if (hide) {
$(element).css("display", "none");
}
});
}
关于javascript - Jquery 按变量向左或向右动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171731/