2011-12-15 10 views
0

たとえば、これが機能する方法はありますか?かなり簡単です:うまく定義済みの関数を.each()で使用できますか?

function resizer() { 
    $(".rezimg").each(sizing($(".rezimg"))) 
    } 
function sizing(im) { 
    im.css({"width": "auto", "height": "auto"}); 
    var imight = im.height(); 
    var imidth = im.width(); 
    var idth = im.parent().width(); 
    var ight = im.parent().height(); 
    var aspecti = (imight/imidth); 
    var aspectc = (ight/idth); 
    if (aspectc>aspecti){ 
     im.css({"width": idth+"px", "height": "auto"}) 
    } 
    else if(aspectc<aspecti){ 
     im.css({"height": ight+"px", "width": "auto"}); 
    } 
    else if(aspectc==aspecti){ 
     im.css({"width": idth+"px", "height": ight+"px"}); 
    } 
} 

私の意図はここではっきりしていることを希望します。 sizing()はイメージの高さをコンテナに比較し、それに応じてフィットする関数です。コード全体でさまざまな点で使いたいと思います。ありがとう。

答えて

1

はい、可能ですが、変更が必要です。以下の作業をする必要があります:

あなたが書いた何
function resizer() { 
    // notice that sizing is not a function call, but a reference 
    $(".rezimg").each(sizing) 
} 

// should also be possible to call it directly on an element 
function sizing(im) { 
    // jQuery each sets the first argument to a number, otherwise it's an element 
    var im = (typeof im === 'number') ? $(this) : im; 

    im.css({"width": "auto", "height": "auto"}); 
    var imight = im.height(); 
    var imidth = im.width(); 
    var idth = im.parent().width(); 
    var ight = im.parent().height(); 
    var aspecti = (imight/imidth); 
    var aspectc = (ight/idth); 
    if (aspectc>aspecti){ 
     im.css({"width": idth+"px", "height": "auto"}) 
    } 
    else if(aspectc<aspecti){ 
     im.css({"height": ight+"px", "width": "auto"}); 
    } 
    else if(aspectc==aspecti){ 
     im.css({"width": idth+"px", "height": ight+"px"}); 
    } 
} 
+0

すなわちサイジング機能内$(this)を使用しての上に反復されている現在の要素を参照することができますforeachの二番目の引数は、あなたの要素を与える必要があります。 –

+0

@Interstellar_Coder私は知っていますが、 'this'はその要素を指していて、foreachループだけでなく、要素の関数を呼び出すこともできます。 – zatatatata

+0

乾杯のメイト、非常に便利です!私はあなたの情報のためにされていなかったと同様に各ループに刈りに来ていただろう。 –

0

関数パラメータとしてサイジングを指定できます。引数はthisで、これは ".rezimg"で表されるdom要素です。詳細についてはSee the docsをご覧ください。

function resizer() { 
    $(".rezimg").each(sizing) 
} 

function sizing(el) { 
    var im = $(el); 
    im.css({"width": "auto", "height": "auto"}); 
    // ... 
} 
0

$(".rezimg").each(sizing($(".rezimg"))) 

$(".rezimg")引数で直接機能sizing()を実行します。しかし、eachの場合、eachは関数としてパラメータを必要とし、sizing($(".rezimg"))は関数ではありません(実際にはundefinedとしてsizing()は何も返しません)ので、実行されません。

どの機能を実行する必要があり、どのパラメータを使用するかは、eachと言います。 sizing.pass($(".rezimg"))として

$(".rezimg").each(sizing.pass($(".rezimg"))) 

したいパラメータが事前にフォーマットさ関数を返す:あなたがすることを行うことができます。

0

確かに、いくつかの方法があります。サイジングを呼び出してサイジングの引数を変更して、以下のようなインデックスと画像要素を受け入れることができます。

function resizer() { 
    $(".rezimg").each(sizing); 
} 
function sizing(index, img) { 
    im.css({"width": "auto", "height": "auto"}); 
    var imight = im.height(); 
    var imidth = im.width(); 
    var idth = im.parent().width(); 
    var ight = im.parent().height(); 
    var aspecti = (imight/imidth); 
    var aspectc = (ight/idth); 
    if (aspectc>aspecti){ 
     im.css({"width": idth+"px", "height": "auto"}) 
    } 
    else if(aspectc<aspecti){ 
     im.css({"height": ight+"px", "width": "auto"}); 
    } 
    else if(aspectc==aspecti){ 
     im.css({"width": idth+"px", "height": ight+"px"}); 
    } 
} 

それとも

function resizer() { 
    $(".rezimg").each(sizing) 
} 

function sizing() { 
    var im = $(this); 
    im.css({"width": "auto", "height": "auto"}); 
    var imight = im.height(); 
    var imidth = im.width(); 
    var idth = im.parent().width(); 
    var ight = im.parent().height(); 
    var aspecti = (imight/imidth); 
    var aspectc = (ight/idth); 
    if (aspectc>aspecti){ 
     im.css({"width": idth+"px", "height": "auto"}) 
    } 
    else if(aspectc<aspecti){ 
     im.css({"height": ight+"px", "width": "auto"}); 
    } 
    else if(aspectc==aspecti){ 
     im.css({"width": idth+"px", "height": ight+"px"}); 
    } 
} 
関連する問題