Jquery 如何为旋转木马中的图像创建预加载程序
我正在寻找一种方法来预装图像为我的网站旋转木马。它们在页面加载时被加载。 有没有现代优雅的方式 我试过但没有成功:Jquery 如何为旋转木马中的图像创建预加载程序,jquery,css,preloader,image-preloader,Jquery,Css,Preloader,Image Preloader,我正在寻找一种方法来预装图像为我的网站旋转木马。它们在页面加载时被加载。 有没有现代优雅的方式 我试过但没有成功: var images = []; $('.gallery_panel').each(function(){images.push($(this).attr('src'));}); $('.gallery_thumbs').each(function(){images.push($(this).attr('src'));}); 和css预加载: <style>
var images = [];
$('.gallery_panel').each(function(){images.push($(this).attr('src'));});
$('.gallery_thumbs').each(function(){images.push($(this).attr('src'));});
和css预加载:
<style>
#preloadedImages {
width: 0px;
height: 0px;
display: inline;
background-image: url(images/....png);
background-image: url(images/....png);
background-image: url(images/....png);
}
</style>
你的第一次尝试是一个良好的开端。基本上是这样做的:
var images = [];
$('.gallery_panel').each(function(){
var src = $(this).attr('src');
var image = new Image();
image.src = src;
images.push(image);
});
$('.gallery_thumbs').each(function(){
var src = $(this).attr('src');
var image = new Image();
image.src = src;
images.push(image);
});
我想知道为什么要将图像声明为数组。我想你的意思是var images={}我也不明白你为什么每次循环都实例化这个对象。为什么不把它们放在一个对象中呢?你必须为每个要预加载的图像创建一个图像对象,我只是把它们推到一个数组中存储。哦,那么是一个对象数组吗?是的,是一个图像数组。创建映像,设置它的源代码应该预加载它,然后我将它转储到一个数组中。