1

我想使用 JS 随机生成一个边距方向,但没有任何效果。怎么了?

这是我的代码

$('.portfolio-img').hover(function(event){
    var str = '';
    var myPos = ['marginLeft', 'marginTop', 'marginBottom', 'marginRight'];
    var thisRand = Math.floor(Math.random() * myPos.length);
    var str = String(myPos[thisRand]);

    $(this).find('img').animate({
        str: '-60px',
    }, 300);
}, function(){
    $(this).find('img').animate({
        str: '0px',
    }, 300);
});
4

2 回答 2

0

Change

$(this).find('img').animate({
    str: '-60px',
}, 300);

to

var o = {};
o[str] = '-60px';
$(this).find('img').animate(o, 300);

As an aside, you can also replace

var str = String(myPos[thisRand]);

with

var str = myPos[thisRand];
于 2013-03-04T13:50:57.767 回答
0

Try this.

   var animOption = {};
   animOption[str] = '-60px';

   $(this).find('img').animate(animOptions, 300);

You will have to cache str in .data and retrieve that in 2nd callback.

于 2013-03-04T13:51:08.920 回答