简体   繁体   English

预加载下一张图片jQuery

[英]Pre-load Next Image jQuery

I am creating a slideshow using nested li's: 我正在使用嵌套李的幻灯片创建幻灯片:

<ul id="ss_images_set">
                <li>
                    <img link="images/koala_1.jpg" />
                    <p class="img_caption">&#169;Copyright</p>
                    <p class="img_author">John Merriam</p>
                    <p class="img_href">http://www.twitter.com</p>
                    <p class="img_hashtag">JohnMerriam</p>
                </li>

I will be uploading new images daily to this slideshow, so there will be many images in the set (hundreds). 我每天都将新图像上载到此幻灯片中,因此该集合中将有很多图像(数百个)。 There are so many pre-loaders out there that I am confused as to what to use to load only the next image. 那里有太多的预加载器,我对于只能加载下一个图像使用什么感到困惑。 The slideshow has an image pre-loader, but I don't think it works properly because the next images are clearly not cached (content below slideshow jumps up and down when going to next image). 幻灯片具有图像预加载器,但我认为它不能正常工作,因为显然没有缓存下一个图像(幻灯片下的内容在转到下一个图像时会上下跳跃)。

Here is the pre-loader script: 这是预加载程序脚本:

function preload_images(){
var pre_image = curr_img - 1;
if(pre_image < 0) pre_image = (tot_elements-1);

var curr_obj = 0;

if(!$('#img_preloaded_'+pre_image).length > 0){
    curr_obj = slideshow[pre_image];
    $('body').append('<img src="'+curr_obj["img_url"]+'" id="img_preloaded_'+pre_image+'" class="preload_box" />');
}

var pre_image = curr_img + 1;
if(tot_elements==pre_image) pre_image = 0;
if(!$('#img_preloaded_'+pre_image).length > 0){
    curr_obj = slideshow[pre_image];
    $('body').append('<img src="'+curr_obj["img_url"]+'" id="img_preloaded_'+pre_image+'" class="preload_box" />');
}

I don't know why it's not caching. 我不知道为什么它不缓存。 Is it written wrong? 是写错了吗? Is it in the wrong spot in the following fiddle? 在下面的小提琴中它是否出现在错误的位置?

Fiddle with JS: http://jsfiddle.net/doobada/9m9eq/ 用JS拨弄琴: http : //jsfiddle.net/doobada/9m9eq/

Current ex. 现任 of slideshow (content below jumps up and down): https://www.assembla.com/code/cfrepo/subversion/node/blob/trunk/index.html#image=JohnMerriam 幻灯片的显示(下面的内容上下跳跃): https : //www.assembla.com/code/cfrepo/subversion/node/blob/trunk/index.html#image=JohnMerriam

Ok, jQuery is not my strong suite (taking the tuts+ courses now), but this shouldn't be taking me 3 weeks to figure out. 好的,jQuery不是我的强项(现在要学习Tuts +课程),但这不应该花我3个星期的时间来弄清楚。

Thanks for your help. 谢谢你的帮助。

var newImageUrl = 'someimage.png';

/* preload an image using jQuery: */
$('<img>').attr( {
    src: newImageUrl
} ).load(function() {
    /* image is loaded and could be used */
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM