1

ウィンドウのサイズが変更されたときに、サイトの壁紙 (オブジェクトの画像) が正しく中央に配置されるように、このスクリプトを作成しました。今では私が望むように動作しますが、ウィンドウのサイズが変更された場合にのみ、ウィンドウのサイズが変更されていない通常の状態では、 img 要素の幅を取得できないようです。左の CSS プロパティ。どうすれば入手できますか?

HTML:

<div class="wallpaper" id="wallpaper">
    <img src=@bgImage.img id="wallpaperImg" style="height:100%; width: auto; position: fixed;">
</div>

JS:

<script type="text/javascript">
    var screenWidth;
    var screenHight;
    var totalHeight;
    var aspectRatio;

    var blah = false; //Ugly hack

    var navbarSize = 41;
    var wallpaper = $("#wallpaperImg");

    function getAspectRatio(h,w){
        return (h-1)/w; //Ugly hack
    }

    function resizeWallpaper(w,h){
        if(typeof(aspectRatio)==='undefined') aspectRatio = @bgImage.aspectRatio; //delivered by server framework

        totalHeight = (h-navbarSize).toString() + 'px';
        wallpaper.css("height",totalHeight);
        wallpaper.css("width","auto");
        wallpaper.css("left",(w/2)-(wallpaper.width()/2)+'px');
        wallpaper.css("top",'41px');


        if(getAspectRatio(wallpaper.height(),wallpaper.width()) > aspectRatio && blah){
            wallpaper.css("width",(w).toString() + 'px')
            wallpaper.css("height","auto")
            wallpaper.css("top",((h/2)-(wallpaper.height()/2))+20.5+'px');
            wallpaper.css("left",'0px');
        }
    }

    resizeWallpaper($(window).width(),$(window).height());
    blah = true;


    $(window).resize(function() {
        resizeWallpaper($(window).width(),$(window).height());
    });
</script> 

私が話していることのクイックアップロード:サイト

4

1 に答える 1

1

サイズを変更するには、画像がダウンロードされるまで待つ必要があります。最初の呼び出しをロード ハンドラで囲みます。

wallpaper.on('load', function() {
    resizeWallpaper($(window).width(),$(window).height());
});
于 2012-11-25T15:13:57.810 に答える