views:

148

answers:

3

Hi there,

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!

+1  A: 

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(); 
         });

    });
Ninja Dude
How is that going to help if you're still wrapping the carousel in `(document).ready`?
Robert Harvey
Hi Robert, At first I thought of using `$(window).load()`, later Changed my mind to `$(document).ready()`. Because on using `(window).load()` it need to wait until page content is loaded **completely**, where as on using `(document).ready()` loading of `Images` is done along with page content http://www.javascriptkit.com/dhtmltutors/domready.shtml
Ninja Dude
A: 

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.

songdogtech
My page definitely has a LOT of things to load, so it certainly is a concern. Right now the items that take the longest to load include: multiple google ad codes, statistical tracking javascripts (ie quantcast, google analytics, compete.com), a "Facebook Fan" box in the sidebar, and Twitter updates, all of which require loading external scripts that slow the page down. I've deemed most of these necessary, though, so while I can focus on optimization, eliminating them isn't really an option right now.
jcarousel_issue
I haven't tried loading jQuery from a CDN, though - we're not using a CDN at all, although we're definitely looking into it. Might be worth a shot.
jcarousel_issue
That's the root of your problem: all that junk that is being loaded. Clean some of that out first. You're probably losing more people than you know (check all those stats to see the bounce rate) because the page loads slow.
songdogtech
I understand what you're saying, however I don't think eliminating everything that has to load is the answer. I've seen numerous sites running the same things we are that load much faster - that's why I think the answer is in optimization or trying different code, not simply deleting things from the page until it loads faster.
jcarousel_issue
@jcarousel: Perhaps you could post two links here; one containing your page, and the other containing a comparison page that loads the way you think it should.
Robert Harvey
A: 

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.

jcarousel_issue