Search code examples
jqueryjplayer

Showing/hiding divs based on jPlayer time update


I'm building a slide presentation with audio, using jPlayer. I want lines of text to appear based on the current time. Each line is wrapped in a div, hidden and given a class. I've added in and out attr to the divs. Here is the code I have so far:

<script type="text/javascript">
$(function() {
$("#jquery_jplayer_1").jPlayer( {
    ready: function () {
        $(this).jPlayer("setMedia", {
            mp3: "media/audio.mp3",
            oga: "media/audio.ogg"
        }).jPlayer("play");
    },
    supplied: "mp3, oga",
    swfPath: "js"
});

$("#jquery_jplayer_1").bind($.jPlayer.event.timeupdate, function(event) {
    currentTime = Math.floor(event.jPlayer.status.currentTime)

    var slidesdiv = $('#slides');

    // Hides all slides to start
    $(slidesdiv).children().addClass('starthidden');

    // stores the number of slides
    var numslides = $(slidesdiv).children().length;

    // adds slidexx class to each div
    $('#slides > div').addClass(function() {
        return 'slide' + $(this).index();
    });

    if (currentTime >= $('#slides > div').attr('in') && currentTime < $('#slides > div').attr('out')) {
        $('#slides > div').fadeIn("fast");
    } else {
        $('#slides > div').fadeOut("fast");
    }
});
});
</script>

and the html:

<div id="slides">
    <!-- Slide 1 -->
    <div in="1" out="3"><h2>Implications</h2></div>
    <div in="5" out="8">Implications have the form</div>
            etc..
</div>

I've gotten pretty close, and the whole #slides appears at 1sec, disappears at 3sec, but I need only the first div to come go, then move on to the next div and it's in/out times. Can anyone help?

Thanks, S


Solution

  • This script might help you a bit:

    <script type="text/javascript">
        $(function () {
            $("#slides").find("div").hide();
    
            var currentInOut = 0;
            var code = setInterval(function () {
                var currDiv = Number() == currentInOut;
                $.each($("#slides").find("div"), function () {
                    if (Number($(this).attr("in")) <= currentInOut && Number($(this).attr("out")) >= currentInOut) {
                        $(this).show();
                        $(this).siblings().hide();
                    }
                });
                currentInOut += 1;
                if (currentInOut >= Number($("#slides").find("div").last().attr("out"))) {
                    clearInterval(code);
                }
            }, 1000);
        });
    
    </script>