在Javascript中显示我的加载程序时遇到问题。实际上,我的资源文件夹中有很多重视频,当我在Javascript中加载页面时,我的图像加载器仅在几秒钟后显示,而我希望首先加载
在我的所有视频资产之前,有没有办法预加载它?
非常感谢提前!
答案 0 :(得分:0)
在任何视频开始下载之前运行的脚本等:
var img = new Image();
img.src = 'http://yoursite.com/preload-this-image.gif';
这应确保在DOM渲染器到达时立即显示图像。
如果在DOM之前有任何重型资产阻止该过程,您可以将它们向前移动并在使用JavaScript加载文档后将其移回。
答案 1 :(得分:0)
您可以在整个页面渲染之前显示加载程序gif:
CSS:
.loader {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 9999;
background: url('/img/page-loader.gif') 50% 50% no-repeat rgb(249,249,249);
}
HTML:
<div class="loader"></div>
JS:
$(window).load(function() {
$('.loader').fadeOut('slow');
});
答案 2 :(得分:0)
您实际上可以将图像转换为base-64编码的字符串,并将其包含在CSS或HTML中。这保证了它可以在其他任何东西之前加载。
这是一个图像转换器:http://webcodertools.com/imagetobase64converter
您可以将其用作内嵌图像或CSS背景图像:
内联HTML:
<img alt="" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH+GkNyZWF0ZWQgd2l0aCBhamF4bG9hZC5pbmZvACH5BAAKAAAAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQACgABACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkEAAoAAgAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkEAAoAAwAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhH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IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQACgAGACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAAKAAcALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==);
答案 3 :(得分:0)
我会将页面源中的图像硬编码为base-64编码的字符串,或者替换为内联svg。 你也可以使用rel =&#34; preload&#34;并看看这是否适合你:
<link rel="preload" href="bg-image.png" as="image" media="(max-width: 800px)">
请参阅:https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content