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

変化する

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

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

余談ですが、交換することもできます

var str = String(myPos[thisRand]);

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

これを試して。

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

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

2番目のコールバックでキャッシュstr.dataて取得する必要があります。

于 2013-03-04T13:51:08.920 に答える