0

これは私が持っているコードです(これまでのところ)。大きな画像の URL から値を直接警告することに成功しました。値をオブジェクトとして別の変数に返すことができるようにしたいのですが、成功していません。私は何を間違っていますか?

$.fn.extend({
        getBgImage: function(callback){
            var path = $(this).css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', '');
            var tempImg = $('<img />');
            tempImg.hide(); //hide image
            tempImg.bind('load', callback());
            $('body').append(tempImg); // add to DOM before </body>
            tempImg.attr('src', path);
            $('#tempImg').remove(); //remove from DOM
        },
        jZoom: function(){
            var swide = $(this).children('.zimg').outerWidth(),
                shigh = $(this).children('.zimg').outerHeight(),
                large = $(this).attr('href');
            $(this).after('<div class="mousearea" style="width:'+swide+'px;height:'+shigh+'px;cursor:move;z-index:99;opacity:0;position:absolute;top:0;left:0;"></div>').parent('.zoom').after('<div class="large" style="background-image:url('+large+');"></div>');
            var limg = $(this).parent('.zoom').next('.large').getBgImage(function(){
                var y = $(this).height(), x = $(this).width();
                alert(x+' '+y); // this alerts the values as expected
                //return {x: x, x: y}; This is what I attempted to return the values
            });
//alert(limg.x); and this is where i check to see if the object are available but get undefined
        }
    });
    $(function(){
        $('.zoomme').each(function(){
            $(this).jZoom();
        });
    });

これは主にそれを行うための進行中の作業ですが、これまでにどこで間違っていたかを知りたいです.

前もって感謝します。

4

1 に答える 1

0

bgImageに引数として渡す関数の戻り値ではなく、関数の戻り値を割り当てていますbgImage

コールバックは渡されても実際には実行されず、代わりにロード ハンドラとしてバインドされるため、その時点で戻り値を取得する方法はありません。

于 2012-11-30T12:29:36.933 に答える