5

Very limited script experience.

I am attempting to get a preloader to cover my images loading in a bootstrap carousel before a second script cycles through them.

This script is as far as I can get.

<!-- Preloader -->
<script type="text/javascript">
    //<![CDATA[
        $(window).load(function() { // makes sure the whole site is loaded
            $('#status').fadeOut(); // will first fade out the loading animation
            $('#preloader').delay(50).fadeOut('slow'); // will fade out the white DIV that covers the website.
            $('body').delay(50).css({'overflow':'visible'});
        })
    //]]>
</script>

and

<!-- Script to Activate the Carousel -->
<script type="text/javascript">
$('#preloader').delay(50).fadeOut('slow', function(){
  $('.carousel').carousel({
      pause: "none",
      interval: 500
  });
});
</script>

The sequence I need is loading animation div covering images loading in "#myCarousel" > images have loaded > covering div fades out > fade out calls:

$('.carousel').carousel({
        interval: 500, //changes the speed
        pause: "none",
    })

Images flick through and stops on final slide.

In the example above, it seems to work – the preloader works, the carousel works too but not at the expected speed; "500" is meant to be half a second. It also seems to break the pause function to, the fact pause is set to "none" seems to be ignored.

After some help from @Shikkediel the script now reads:

<script type="text/javascript">
$(window).on('load', function() {

    $('#status').fadeOut()
    .queue(function() {

        $('#preloader').delay(50).fadeOut('slow', function() {
            $('#myCarousel').attr('data-ride', 'carousel');
            $('.item').first().addClass( 'active' );
            $('body').delay(50).css({'overflow':'visible'});
            $('.carousel').carousel({
            });
        });
        $(this).dequeue();
    });
});
</script>

– the speed and pause are now set in the data attributes, which is great to have them set in there out of the way.

However, the preloader still does not pre load the images!

This is the test I have running: http://maxsendak.com/test/pu_v2/max_page.html

a1652
  • 103
  • 1
  • 7
  • Maybe `$('.carousel').carousel({'loop': true, ... })`? I don't see `pause` as an option, unless I'm looking at a different carousel. The `fadeOut()` method is asynchronous by the way. – Shikkediel Apr 19 '15 at 19:56
  • There is a pause – http://getbootstrap.com/javascript/#carousel – setting this to 'none' stops the carousel pauses, which what stops it on the final slide as I need. – a1652 Apr 19 '15 at 20:02
  • Yep, different one. That would have been good to mention. I guess the issue is in it not working at all as expected then? It is not very clear from the question. – Shikkediel Apr 19 '15 at 20:07
  • 1
    Sorry, I will try and understand the problem more and edit the question. – a1652 Apr 19 '15 at 20:08
  • Putting a script together but saw some room for improvement. – Shikkediel Apr 19 '15 at 20:22
  • I bet there is plenty of room! – a1652 Apr 19 '15 at 20:24
  • Gotta say I'm a bit at a loss here - didn't realise it was about Bootstrap at first. But the page you referred to has conflicting info with this : http://stackoverflow.com/q/17332431/3168107. – Shikkediel Apr 19 '15 at 20:58
  • Info at the link seems inaccurate. The information is out of date / inaccurate – 'interval' does set the speed, that being the amount of time one slide shows before the next. I have a still working example of that still effecting change – it only breaks *after* the preloader is involved. – a1652 Apr 19 '15 at 21:00
  • Your comment actually sent me back to the Bootstrap link I posted above, I found that I could put both `pause: 'false'` and `interval: 500` in the data attributes as taking them out of the revised code you posted – so thank you. The preloader still doesn't work though... Maybe the preloader is a fake. – a1652 Apr 19 '15 at 21:18
  • Sorry I couldn't be of more help here - and for comments I decided to remove before I noticed you replied. Thought it was more a general jQuery question, can't test Bootstrap myself so I'm left googling too. – Shikkediel Apr 19 '15 at 21:22
  • 1
    I appreciate the input though, thanks. I will update the question to include me test page. – a1652 Apr 19 '15 at 21:26

1 Answers1

3

This was the first draft, a bit of optimised script :

$(window).on('load', function() {

    $('#status').fadeOut()
    .queue(function() {

        $('#preloader').delay(50).fadeOut('slow', function() {
            $('#myCarousel').attr('data-ride', 'carousel');
            $('.item').first().addClass('active');
            $('body').delay(50).css({'overflow':'visible'});
            $('.carousel').carousel({
                interval: 500,
                pause: 'none'
            });
        });
        $(this).dequeue();
    });
});

But it didn't quite have the expected result - the images are set as background and not detected by the onload event. I would propose preloading, appending to the document and hiding. This should cache them for the slider. Doing an Ajax call doesn't seem to fit well here :

$(document).ready(function() {

    var path = 'http://www.maxsendak.com/test/pu_v2/img/people/max/';

    for (var i = 1; i <= 5; i++) {
        $('<img class="preload" src="' + path + i + '.jpg" alt=""/>').appendTo('body');
    }

    $('.preload').each(function() {
        $(this).one('load', function() {
            $(this).hide();
        });
    });

    $(window).on('load', function() {

        $('#status').fadeOut()
        .queue(function() {

            $('#preloader').delay(50).fadeOut('slow', function() {
                $('#myCarousel').attr('data-ride', 'carousel');
                $('.item').first().addClass('active');
                $('body').delay(2500).css({'overflow':'visible'}); // after slider finishes
                $('.carousel').carousel();
            });
            $(this).dequeue();
        });
    });
});

That bit of script could be left out of course if the images weren't background, the onload event should then be enough and prevent the flashing in between slides.

Minor update - according to this article Firefox can have some issues (version 37 on Windows 7 desktop at least) with the style of the preloaded images not matching that of the targeted background images. So I've added the relevant style I can see on the slider here which should be enough (also made appending the images a bit more readable) :

for (var i = 1; i <= 5; i++) {

    $('<img class="preload" alt=""/>')
    .attr('src', path + i + '.jpg')
    .css({'height': '100%', 'position': 'relative', 'left': 0})
    .appendTo('body');
}

Hope that's the final detail for a smooth cross browser functionality.

Shikkediel
  • 5,195
  • 16
  • 45
  • 77
  • The optimised script was helpful. I didn't know the images being background-images could a problem for a preloader - Ajax, though, is well beyond my capabilities. – a1652 Apr 19 '15 at 22:49
  • I'm no wizz at it either so I chose to do a preload by appending the images to the document and then hiding them with plain jQuery. That ought to work as well, Ajax doesn't seem to be the greatest tool for this anyway. Let me know if it has the desired result (did live test a bit). – Shikkediel Apr 19 '15 at 23:11
  • That's interesting, great to do it without Ajax – a lot of this worked, except the carousel did not fire, it doesn't cycle through the images as expected, I've put the carousel call in "after slider finishes" and this, although I am still testing, seems to be _the_ answer. – a1652 Apr 20 '15 at 09:14
  • Oh yeah, accidentally took out the line that calls the slider altogether. Lol, let me put that back in. – Shikkediel Apr 20 '15 at 10:56
  • Really smart answer – amazing that you could take on all the pitfalls working through the solution, without resting on an answer that didn't fully solve the issue. Nice one! Works a treat. – a1652 Apr 20 '15 at 11:16
  • 1
    Glad I could help out. Seems to be working well on the major browsers but I'm still noticing the incidental flash on Firefox when a hard refresh is done. That apparently has to do with it being quirky when not all style has been set yet : https://kylekelly.com/posts/2014/04/08/firefox-flickering.html. So that should be solvable as well if you'd like to tackle it (gimme a shout if you need help). Cheers. – Shikkediel Apr 20 '15 at 11:36
  • I can't _even_ reproduce the issue, I'm on Firefox 24.0 Mac OSx 10.9.5 – a1652 Apr 20 '15 at 12:25
  • 1
    Okay, must work more accurate than a Windows 7 Firefox 37 combo then. I guess it would be a small fix though - I'll post a minor update. In my idea it would be more for backwards compatibility than anything else. I can't imagine this behaviour to stay in the browser spec (but it seems they've been slacking off somewhat at Mozilla in recent times). – Shikkediel Apr 20 '15 at 12:42