在 jQuery 中预加载背景图像
我在预加载背景图像时遇到问题。这是我的尝试,但似乎仍然不起作用 - 即。当我调用 showAjaxLoader("#mytarget") 时,看到图像仍然有延迟。有人有什么建议吗?
CSS:
div.entryform-ajax-loader {
background-image:url(../images/preloader-entry-form.gif);
}
脚本:
var ajax_loader = $('<div />').attr('class', 'entryform-ajax-loader');
function showAjaxLoader(target)
{
$(target).append(ajax_loader);
}
function hideAjaxLoader(target)
{
ajax_loader.remove();
I'm having trouble pre loading background images. This is my attempt but still seems to not be working - ie. there is still a delay seeing the image when i call showAjaxLoader("#mytarget"). Does anybody have any suggestions?
CSS:
div.entryform-ajax-loader {
background-image:url(../images/preloader-entry-form.gif);
}
Script:
var ajax_loader = $('<div />').attr('class', 'entryform-ajax-loader');
function showAjaxLoader(target)
{
$(target).append(ajax_loader);
}
function hideAjaxLoader(target)
{
ajax_loader.remove();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将一个 div 添加到您的 head 中,其中包含图像的源,在这种情况下,图像将在实际页面加载之前加载。
Add a div to your head which has the source as your image, in this case, image will be loaded before the actual page loads.
在页面正文的头部或者如果您需要 JQuery 解决方案
In the head of page body or if you need JQuery solution
作为替代解决方案,您可以将此脚本添加到您的文件中,
这将在页面加载开始时加载图像。因此,当执行 showAjaxLoader 函数时,图像将毫无延迟地加载。
As an altermative solution, you may add this script to your file,
which will load the image at the beginning of page load. As a result when the function showAjaxLoader will be executed, the image will be loaded without any delay.