2016-10-08 6 views
-1

私のコードにいくつかのエラーがあります。誰かが私を助けてくれるかもしれない。私はこのエラーを取得しています:jquery関数でCSSプロパティを適用する

SyntaxError: missing formal parameter: $(this).css("position" , "relative");

$(document).keydown(function(e) { 
 
    var count = 0; 
 

 
    if (e.keyCode == 40) { 
 
    count++; 
 
    } 
 

 
    if (count == 1) { 
 
    $('#box').animate({ 
 
     borderSpacing: -90 
 
    }, { 
 
     step: function(now, fx) { 
 
     $(this).css('-webkit-transform', 'rotate(' + now + ' deg)'); 
 
     $(this).css('-moz-transform', 'rotate(' + now + ' deg)'); 
 
     $(this).css('transform', 'rotate(' + now + ' deg)'); 
 
     }, 
 
     duration: 'slow' 
 
    }, 'linear'); 
 
    } else { 
 
    $('#box').animate({ 
 
     height: '10%' 
 
    }, { 
 
     $(this).css("position", "relative"); 
 
     $(this).css("margin-top", "100px"); 
 
     $(this).css("width", "30%"); 
 
     $(this).css("background-color", "tomato"); 
 
     $(this).css("margin-left", "20%"); 
 
     $(this).css("align-content", "center"); 
 
    }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="box"> 
 
    <div id="box2"></div> 
 
</div>

+0

[にSyntaxError:欠落している仮パラメータ](http://stackoverflow.com/questions/25290794/syntaxerror-missing-formal-parameter) – Mohammad

+1

あなたは匿名関数ラッパーを欠場、チェックdoc –

+3

'jQuery.fn.animate'の2番目の引数は関数でなければなりません – synthet1c

答えて

0

あなたがAPIに間違った方法を使用している、animateの第2引数のための機能を使用して、それが動作します。

代わりにこれを試してみてください:

$('#box').animate({ 
    height: '10%' 
}, function() { 
    $(this).css("position", "relative"); 
    $(this).css("margin-top", "100px"); 
    $(this).css("width", "30%"); 
    $(this).css("background-color", "tomato"); 
    $(this).css("margin-left", "20%"); 
    $(this).css("align-content", "center"); 
}); 
+1

あなたはこの機能に精通していますか(複数のCSSバールを設定するためのオブジェクトのようなJSONを使用しています)? '$(this)).css({" position ":"相対 "、" margin-top ":" 100px "、....});'それはもっと洗練されたものになるでしょう - http://api.jquery.com/ css /#css-properties –

+1

実際には、[** this **](https://jsfiddle.net/mh9wbyde/)は完全に有効で、オブジェクトは2番目の引数として使用されますが、OPにはオブジェクトがありませんそれはちょうどナンセンスでいっぱいの2つの括弧です。 – adeneo

+0

私はそれに精通していますが、私の目標は、コードレビューサイトのためのものであることは確かですが、コード改善に関するフィードバックを提供しないように問題を解決することでした。 – GillesC

0

jQuery.fn.animateの最後の引数は、関数であるべき。あなたがそれを必要とするならば。

$(selector).animate({...styles...}, time, easing, function(){ /* callback */}) 

また、オブジェクトを使用して、すべてのCSSプロパティを同時に設定することができます。

$(this).css({ 
 
    position: 'relative', 
 
    marginTop: '100px', 
 
    width: '30%', 
 
    backgroundColor: 'tomato', 
 
    marginLeft: '20%', 
 
    alignContent: 'center' 
 
})

関連する問題