php/Ajax - Best practice for pre-loading images

前端 未结 4 869
旧时难觅i
旧时难觅i 2021-01-01 06:58

I have put together a script which is very much like the flickr photostream feature. Two thumbnails next to each other, and when you click the next or prev links the next (o

4条回答
  •  没有蜡笔的小新
    2021-01-01 07:08

    To preload an image from Javascript, you don't need to do anything that sounds like AJAX or JSON. All you need is this:

    var img = new Image();
    img.src = "http://example.com/new/image.jpg";
    

    The browser will quite happily load the image in the background, even though it's not displayed anywhere. Then, when you update the src field of another (displayed) image tag, the browser will immediately show the part of the image it's already loaded (hopefully all of it).

提交回复
热议问题