如何将数组值放入动画中

How to put an array value into an animation?

本文关键字:动画 数组      更新时间:2023-09-26

我在对象动画中尝试从数组中插入随机值时遇到了问题,知道如何解决这个问题吗?

var modalExitDir = Array('top','right','bottom','left');
//random value from modalExitDir
var item = modalExitDir[Math.floor(Math.random()*modalExitDir.length)];
$('#modal-close-btn').click(function(){
    $('#jobs-modal').animate({
        item : -100 + '%',
        opacity : 0.5},
        500, function() {
        $(this).removeAttr('style');
    });
});

感谢

我能想到的唯一方法是使用switch()检查item变量,该变量也应该出现在点击函数中:DEMO

var modalExitDir = Array('top','right','bottom','left');
//random value from modalExitDir
$('#modal-close-btn').click(function(){
    var item = modalExitDir[Math.floor(Math.random()*modalExitDir.length)];
    switch(item){
        case 'top':
            $('#jobs-modal').animate({top:'-100%'},500);
            break;
        case 'right':
            $('#jobs-modal').animate({left:'100%'},500);
            break;
        case 'bottom':
            $('#jobs-modal').animate({top:'100%'},500);
            break;
        case 'left':
            $('#jobs-modal').animate({left:'-100%'},500);
            break;
    }
    $('#jobs-modal').animate({
        opacity : 0.5},
        500, function() {
        $(this).removeAttr('style');
    });
});

试试这个

var modalExitDir = Array('top','right','bottom','left');
var item;
$('#modal-close-btn').click(function(){
 item= modalExitDir[Math.floor(Math.random()*modalExitDir.length)];
    $('#jobs-modal').animate({
        item : -100 + '%',
        opacity : 0.5},
        500, function() {
        $(this).removeAttr('style');
    });
});