简体   繁体   English

js文件的加载顺序

[英]Load order of js files

I can't seem to get the preloader (Query Loader 2) to load before everything else on the page. 我似乎无法让预加载器(查询加载器2)先加载页面上的其他所有内容。 When I refresh the page the images in the full screen slider display block down the page, then the query loader starts. 当我刷新页面时,全屏滑块显示中的图像会阻止页面向下,然后查询加载器启动。 Is there a way to start the preloader before everything else on the page? 有没有一种方法可以在页面上的其他所有内容之前启动预加载器?

I use stack overflow alot and normally find the answer to my question but with my limited knowledge of javascript this one has got me stumped. 我经常使用堆栈溢出,通常会找到问题的答案,但是由于我对javascript的了解有限,这使我感到困惑。 Things I've tried: Putting the call to queryloader2 right at the top of the page in the header. 我尝试过的事情:将对queryloader2的调用放在页眉顶部的页面顶部。 Putting the call to the slider scripts at the bottom of the page so they load after the preloader. 将调用放在页面底部的滑块脚本上,以便它们在预加载器之后加载。 Changing the z-index of the preloader to higher than the slider. 将预加载器的z索引更改为高于滑块。 jQuery.getScript() which loaded the scripts in order but the slider still displayed block down the page and then the preloader started. jQuery.getScript()按顺序加载了脚本,但是仍然显示滑块,阻止了页面,然后启动了预加载器。

I'm thinking its to do with the load order but if you have any ideas as to what I'm doing wrong here your help would be much appreciated. 我认为这与加载顺序有关,但是如果您对我在做什么错有任何想法,在此您的帮助将不胜感激。

I've put a link to my site as I didn't know which piece of code to put on here and so you can see the way the preloader and slider load the wrong way round http://stavriaphotography.com 我已经链接到我的网站,因为我不知道要在此处放置哪段代码,因此您可以看到预加载器和滑块以错误的方式加载的方式http://stavriaphotography.com

The site is quite heavy on external scripts. 该站点非常重视外部脚本。 Here's how loading resources in browsers work: 这是在浏览器中加载资源的工作方式:

Images are loaded asynchronously, this means browser doesn't wait for the image to load before continuing further down the DOM , however 图片是异步加载的,这意味着浏览器在继续深入DOM之前不会等待图片加载。

JavaScript is loaded synchronously and you can not load the next one before the previous is loaded. JavaScript是同步加载的,您不能在前一个加载之前加载下一个。

jQuery $(document).ready() function fires only when the DOM is completly loaded. jQuery $(document).ready()函数仅在完全加载DOM时触发。

Here's what going on, on your site: 这是您网站上发生的事情:

You load jQ and queryLoader in the head and prepare to call it when DOM is ready. 您将jQ和queryLoader加载到头部,并准备在DOM准备好时调用它。 The scripts in the footer take time to load and are delaying the $(document).ready() function call. 页脚中的脚本需要花费一些时间才能加载,并且延迟了$(document).ready()函数的调用。 In the mean time you have images in your body and since they are loaded asynchronously the browser begins loading them before the queryLoader is ready to execute. 同时,您体内有图像,并且由于图像是异步加载的,因此在queryLoader准备执行之前,浏览器开始加载图像。

The most simple solution in your case would be to move all the external scripts to your html header , however not a very practical one. 在您的情况下,最简单的解决方案是将所有外部脚本都移到html标头中 ,但这不是一个非常实用的方法。

I'd suggest reading up a bit on JavaScript and splitting up you site into multiple files for faster loading. 我建议您阅读一些JavaScript知识,然后将您的网站分成多个文件,以加快加载速度。

Some pointers: jQuery.ajax() and Handlebars.js or if you really want to go crazy dive into Backbone.js with RequireJS for asynchronous javascript loading. 一些指针:jQuery.ajax()和Handlebars.js,或者如果您真的想疯狂地使用RequireJS进入Backbone.js进行异步javascript加载。

Hope this helps! 希望这可以帮助!

Put Jquery Library first 首先将jQuery库

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

then add your other JS files 然后添加其他JS文件

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

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