1

I am using a jquery plugin called 'typed' that types out sentences, one after the other. this is great but I was wondering if it would be possible to pause the code until the text area was visible in the browser window?

As I want people to be able to see the first sentence that is typed, but currently you would miss the first sentence as it's not visible from the top of the page.

This is the page I'm working on: http://jakdesign.webflow.com/

This is the code in the head of the html:

$(document).ready(function( $ ) {
    $("#hero-text").typed({
        strings: ["First sentence.", "Second sentence."],
        typeSpeed: 0
    });
});

This is the plugin: https://rawgithub.com/morr/jquery.appear/master/jquery.appear.js

2 Answers2

1

You want to initialize typed when element becomes visible, don't you? Then try something like this:

$(document).ready(function( $ ) {
    $("#hero-text").one("appear", function() {
        $(this).typed({
            strings: ["First sentence.", "Second sentence."],
            typeSpeed: 0
        });
    });
});

Of course, this requires jQuery.appear plugin.

nrodic
  • 3,026
  • 3
  • 33
  • 38
0

document ready is fired when the DOM has loaded, so all widget attributes are not retrieved completely.

window onload waits for the assets in the page to be completely loaded - so information and attributes of tag are available completely.

So this will work for you:

function typed() {
    $("#hero-text").typed({
        strings: ["First sentence.", "Second sentence."],
        typeSpeed: 0
    });
}

    <body onload="typed()">

OR In script do this

window.onload = function(){
    $("#hero-text").typed({
            strings: ["First sentence.", "Second sentence."],
            typeSpeed: 0
        });
}
DarkHorse
  • 2,740
  • 19
  • 28
  • Better jQuery syntax: `$(window).on("load", function(){...});`. See also http://api.jquery.com/load-event/ – Aaron Digulla Oct 03 '13 at 09:08
  • I sometimes avoid using jQuery wherever I can.. for simple function like window.onload. Its just wrapper over window.onload.. :) – DarkHorse Oct 03 '13 at 09:12