0

I have a problem when trying to insert a random value from an array in an object animation, any idea how to solve this problem?

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');
    });
});

Thanks

2 Answers 2

1

the only way I can think of is using a switch() to check the item variable which should also appear inside the click function: 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');
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! Works perfectly! Thanks a lot man! IF anybody want to see the example working: jsfiddle.net/g0utuxc7/1
you're welcome, it's a really nice idea, the thing you did! ;)
1

Try This

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');
    });
});

2 Comments

Here ir what i'm trying to do(your code is implemented): jsfiddle.net/g0utuxc7 , click in any "image" then it will open a modal, the problem is in cle close action, when close the direct to go should be random but it is nos working... Understand what i'm trying to say?
Sorry I did not understand (((

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.