0

I am attempting to make a slideshow out of text, wherein one piece of text fades out, and another fades in, and so on. I have it working for the most part, but there is a small issue.

When the page is first loaded, all of the pieces of text are displayed at once. Each one fades in turn, and once they have all faded once they function as I want. I have used:

(function langFade() {

var lang = $('.lang, .first');
var langIndex = -1;

function showNextLang() {
    ++langIndex;
    lang.eq(langIndex % lang.length)
        .fadeIn(1500)
        .delay(2000)
        .fadeOut(1500, showNextLang);
}

showNextLang();

})();

as described here, but this is causing the problem described above. I've attempted using CSS to hide all but the first piece of text when the site is loaded, but this isn't doing the trick. My suspicion is that the issue is with the HTML - it is rather different to the demo. I have created a fiddle to demonstrate what I mean.

Is there any way of fixing this, either through modifying the JavaScript, or the HTML?

2 Answers 2

3

Try to hide the others at first with .not(':eq(0)').hide()

(function langFade() {

var lang = $('.lang, .first'),
    langIndex = -1;

lang.not(':eq(0)').hide();

function showNextLang() {
    ++langIndex;
    lang.eq(langIndex % lang.length)
        .fadeIn(1500)
        .delay(2000)
        .fadeOut(1500, showNextLang);
}

showNextLang();

})();

Can also use .not(':first').hide() which may be a little easier to read.

Sign up to request clarification or add additional context in comments.

Comments

1

Just hide them initially. And this can be done in pure CSS. Add following class:

h2.first, h2.lang {
    display: none 
}

Demo: http://jsfiddle.net/XbWJS/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.