在首屏使用 JCarousel - 在页面加载完成之前加载 JCarousel?

发布于 2024-09-12 12:38:27 字数 465 浏览 11 评论 0原文

我目前正在使用 JCarousel 来突出显示 Wordpress 中的“特色帖子”。轮播显示在首屏上方,作为标题的一部分。一旦加载,它就可以很好地工作,但由于我们在包含许多元素的大页面的首屏上方使用它,因此它必须等待整个页面加载,然后才能启动并显示我们的特色帖子。这意味着它会在“正在加载”gif 中停留 5-10 秒,大多数用户只会滚动它而不是等待它加载。

<script type="text/javascript"> 
    jQuery(document).ready(function(){ 
        jQuery('#mycarousel').jcarousel({ scroll : 2 }); 
    }); 
</script>

这种缓慢的加载很麻烦,因此,我想先加载轮播,这样它就会在页面的其余部分加载之前显示出来。我有什么办法可以做到这一点吗?

任何建议将不胜感激。谢谢!

I'm currently using JCarousel to highlight "featured posts" in Wordpress. The carousel displays above the fold, as part of our header. It works well once it loads, but since we're using it above the fold on a large page with many elements, it has to wait for our entire page to load before it will initiate and display our featured posts. This means it sits there with a "loading" gif for 5-10 seconds, and most users will just scroll by it rather than wait for it to load.

<script type="text/javascript"> 
    jQuery(document).ready(function(){ 
        jQuery('#mycarousel').jcarousel({ scroll : 2 }); 
    }); 
</script>

This slow loading is bothersome, and therefore, I'd like to have the carousel load first, so it will show up before the rest of the page has loaded. Is there any way for me to do this?

Any suggestions would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兮子 2024-09-19 12:38:59

作为后续,这就是我最终解决问题的方法:

由于侧边栏中的 Facebook 小部件和 Twitter 小部件需要永远加载,并且在轮播之前加载,因此我将它们转换为外部 javascript(通过加载 Facebook iframe)使用此方法的 JavaScript: http://www.seomofo.com/wordpress/ tweetmeme-retweet-button.html)并在 window.onload 上调用它们,因此它们将在最后开始加载。

现在,jcarousel 在 Facebook 和 Twitter 的小部件之前加载,使其显示得更快,并且加载时间似乎要少得多。尽管我相信我的整体页面加载时间是相同的,但我至少找到了一种重新排列内容的方法,以便页面顶部的项目会显示在侧边栏中加载缓慢的小部件之前。

感谢您的帮助!很高兴解决了这个问题。

Just as a followup, here's what I ended up doing to solve my problem:

Since the Facebook widget and Twitter widgets in my sidebar took forever to load, and were loading before the carousel, I turned them into external javascripts (loaded the Facebook iframe through a javascript using this method: http://www.seomofo.com/wordpress/tweetmeme-retweet-button.html) and called them on window.onload, so they would start loading dead last.

Now the jcarousel loads before both Facebook and Twitter's widgets, making it show up sooner and appear to take much less time to load. Although I believe my overall page load time is the same, I at least found a way to rearrange things so the items at the top of my page would show up before the slow-loading widgets in the sidebar.

Thanks for all your help! Glad to have this resolved.

慢慢从新开始 2024-09-19 12:38:56

我想看看是什么导致您的页面加载时间如此之长。发布您的 URL 或使用 Firebug 查看您的页面加载和资源。您的图片太大了吗?您是否加载了太多脚本或重复的 jQuery 副本?您是否尝试过使用 使用 Google Libraries « WordPress 插件?

您可以在滑块下方的元素上触发 display:none ,直到页面加载;即:

document.write('<style type="text/css">element{display:none}</style>');
jQuery(function($) {
$('element').css('display','block');
});

但我首先要弄清楚为什么页面加载很慢。

I'd see what is making your page take so long to load. Post your URL or use Firebug to look at your page load and resources. Are your images too large? Are you loading too many scripts or duplicate copies of jQuery? Have you tried loading jQuery from a CDN like Google with Use Google Libraries « WordPress Plugins?

You can trigger a display:none on elements below the slider until the page loads; i.e.:

document.write('<style type="text/css">element{display:none}</style>');
jQuery(function($) {
$('element').css('display','block');
});

But I'd figure out why the page load is slow first.

北方的巷 2024-09-19 12:38:50

预加载 #mycarousel 内容怎么样?如果您查看jCarousel Plugin的源代码,作者没有编写任何用于预加载内容的代码块(例如Images)。因此,请尝试在页面中的其余元素仍在加载之前预加载选择器的内容,

我可以向您展示一个示例,

    $(document).ready(function() {
         var img;   
         $('#mycarousel').find('img').each(function() {
              img = new Image();
              img.src = $(this).attr('src');   //preloads your Images
         });

         $(img).load(function() {  //perform action after the last Image is loaded
              $('#mycarousel').jcarousel(); 
         });

    });

what about preloading of #mycarousel contents ? If you look into the source of jCarousel Plugin, the author haven't written any block of code for preloading the contents ( for example Images ). So try preloading the contents of your selector before rest of the elements in your page are still loading

I can show you an example,

    $(document).ready(function() {
         var img;   
         $('#mycarousel').find('img').each(function() {
              img = new Image();
              img.src = $(this).attr('src');   //preloads your Images
         });

         $(img).load(function() {  //perform action after the last Image is loaded
              $('#mycarousel').jcarousel(); 
         });

    });
暗藏城府 2024-09-19 12:38:46

使用 CDN 的原因是浏览器能够“并行”加载 JS,因此应该加以利用。

iframe 的使用可能是最糟糕的机制,应该被视为“黑客”而不是任何东西的“修复”(一般而言)。

考虑使用 LazyLoad、CDN 和 JS Staggering - 即在 HEAD 中加载库文件,在页脚区域中加载其余 JS。

The reasoning for CDN usage is for the browsers to be able to load the JS in 'parallel' and should be utilized.

An iframe usage is probably the worst mechanism and should be considered a 'hack' not a 'fix' for anything (in general).

Consider using LazyLoad, CDN, and JS Staggering - i.e. loading the Library files in HEAD and the rest of the JS in Footer area.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文