20

背景に大きな画像を使用する Web ページがあります。ダウンロードしたら、jQueryを使用して画像をロードすることを望んでいました(基本的に、bing.comが背景画像をロードする方法)。これはjQueryで可能ですか?もしそうなら、お勧めのプラグインはありますか?

4

4 に答える 4

25

最初に画像をロードし、ロードが完了したら、背景画像として設定できます。そうすれば、ブラウザは(うまくいけば)背景画像を再ダウンロードする代わりにキャッシュから読み込みます。プラグインとしてリクエストしたとおり:

 $.fn.smartBackgroundImage = function(url){
  var t = this;
  //create an img so the browser will download the image:
  $('<img />')
    .attr('src', url)
    .load(function(){ //attach onload to set background-image
       t.each(function(){ 
          $(this).css('backgroundImage', 'url('+url+')' );
       });
    });
   return this;
 }

次のように使用します。

 $('body').smartBackgroundImage('http://example.com/image.png');
于 2009-06-10T17:47:36.563 に答える
11

この記事は役に立つかもしれません。そこからコピー:

HTML

<div id="loader" class="loading"></div>

CSS

DIV#loader {
  border: 1px solid #ccc;
  width: 500px;
  height: 500px;
}

/** 
 * While we're having the loading class set.
 * Removig it, will remove the loading message
 */
DIV#loader.loading {
  background: url(images/spinner.gif) no-repeat center center;
}

Javascript

// when the DOM is ready
$(function () {
  var img = new Image();

  // wrap our new image in jQuery, then:
  $(img)
    // once the image has loaded, execute this code
    .load(function () {
      // set the image hidden by default    
      $(this).hide();

      // with the holding div #loader, apply:
      $('#loader')
        // remove the loading class (so no background spinner), 
        .removeClass('loading')
        // then insert our image
        .append(this);

      // fade our image in to create a nice effect
      $(this).fadeIn();
    })

    // if there was an error loading the image, react accordingly
    .error(function () {
      // notify the user that the image could not be loaded
    })

    // *finally*, set the src attribute of the new image to our image
    .attr('src', 'images/headshot.jpg');
});
于 2009-06-10T17:49:32.440 に答える
1

CSS の背景画像は使用できません。これは、画像の読み込みにイベントを関連付けることができないためです (私の知る限り)。

だから私はそれを試してみますが、テストしていません:

HTML:

<body>
<div class="bgimage"><img src="/bgimage.jpg" ></div>
<div>
  ... content ...
</div>
</body>

CSS:

.bgimage { position: absolute: }

Javascript:

$(function() {
   $(".bgimage")
      .css("opacity",0);
      .load(function() { $(this).fadeIn(); });
});
于 2009-06-10T17:47:58.107 に答える
-2

次のトリックを使用できます。少し汚い場合は申し訳ありません。

脚本 :

        jQuery.preloadImages = function() {
        for (var i = 0; i < arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
            $('.box1').attr('preloadURL', arguments[0]);
        }
    }

    $.preloadImages("sky.jpg");

    $(document).ready(function() {
        if ($('.box1').attr('preloadURL') == 'sky.jpg') {
            $('.box1').css({ 'background-image': 'url(sky.jpg)' },$('.box1').fadeIn(1000));
        }
    });

マークアップ :

<div class="Content">
        <label>Search Bing</label>
        <input type="text" />
        <input type="button" value="search" />
    </div>
<div class="box1"></div>

CSS:

.box1 {background-repeat:no-repeat; width:800px; height:600px; display:none; position:relative;}
    .Content { position:absolute; left:20px; top:20px; z-index:100;}
    .Content label { color:White;}

お役に立てれば

上の小さなサンプル: http://xgreen.co.uk/test.htm

于 2009-06-10T20:25:04.577 に答える