【问题标题】:How to change animation name/type in jquery by a string name如何通过字符串名称更改jquery中的动画名称/类型
【发布时间】:2023-03-13 16:15:03
【问题描述】:

这就是概念。有什么可能的方法来做到这一点
这是我的代码:

var animationName='slideUp';
if(true){
animationName='fadeOut';
}

$('.className').animationName(200,function(){
     //callback
});

【问题讨论】:

标签: jquery jquery-animate fadeout slideup


【解决方案1】:

您可以使用eval() 将字符串作为javascript 表达式执行。在eval() 中,您可以使用变量作为函数名。

var animationName = "slideUp";
eval("$('div')."+ animationName +"(200, function(){})");
div {
    width: 50px;
    height: 200px;
    background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2014-06-04
    • 2021-10-10
    • 1970-01-01
    • 2016-11-02
    • 2018-04-11
    相关资源
    最近更新 更多