预加载图像的功能?
我成功地使用此 JavaScript 在我的网站上预加载了图像:
loveHover = new Image();
loveHover.src = "http://mypage.com/images/love-hover.png";
是否有一种简单的好方法将这个东西打包到函数中?像这样的东西:
function preloadImage(image) {
var image = new Image();
var path = "http://mypage.com/images/";
image.src = path + image;
}
I'm successfully pre-loading an image on my website with this JavaScript:
loveHover = new Image();
loveHover.src = "http://mypage.com/images/love-hover.png";
Is there an easy an good way to pack this thing into a function? Something like:
function preloadImage(image) {
var image = new Image();
var path = "http://mypage.com/images/";
image.src = path + image;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使其在早于 9 的 IE 版本中工作,请参阅 Array .forEach 兼容性部分 了解说明。
To get this to work in IE versions earlier than 9, see the Array.forEach Compatibility section for instructions.
该函数的独特部分是 src(图像链接)。所以以此作为论据。
然后,如果您有多个 url,请将它们存储在一个数组中:
并使用循环预加载图像:
Well the unique part of the function would be the src (link to image). So make that the argument.
Then if you have multiple urls store them in an array:
And preload the images with a loop:
您是否尝试过完全不使用 javascript?
http://perishablepress.com/press/2008/06/14/a-way-to-preload-images-without-javascript-that-is-so-much-better/
Have you tried not using javascript at all?
http://perishablepress.com/press/2008/06/14/a-way-to-preload-images-without-javascript-that-is-so-much-better/