Javascript,暂停打字机

时间:2014-07-29 12:21:40

标签: javascript jquery html

我对我的javascript有疑问。我面临的问题是,我的单页网站。我有一个在div中运行的打字机.js。我想要完成的是,当你向上或向下滚动时,打字机会冻结' (暂停)当您滚动回页面时,打字机会再次继续。

HTML打字机

    <div class="type-o">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pellentesque consectetur pulvinar. Pellentesque a dapibus nisi.</p>
    </div>

Javascript onepage

//section 1
if(anchorLink == 'Illusiveman'){
    $(".type-o").typewriter('typestart');
}

if ((index == 2 && direction == 'down') || (index == 2 && direction == 'up')){
    $(".type-o").typewriter('typepause');
}

Javascript打字机

    $.fn.typewriter = function(a) {
        this.each(function() {
            var $ele = $(this), str = $ele.text(), progress = 0;
            $ele.text('');
            var timer = setInterval(function() {

                if(a = 'typestart'){
                    $ele.text(str.substring(0, progress++) + (progress & 1 ? '_' : ''));
                    if (progress >= str.length) clearInterval(timer);

                }else if(a == 'typepause'){
                    clearInterval(timer);
                }   

            }, 100);
        });     
    return this;
};

我现在拥有的是,如果您保留内容..打字机会继续,但当您滚动回内容时,打字机会再次启动,就像您在单词中启用插入一样。我现在已经不在思想了,所以我需要一些帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

<强>已更新 在更正代码后(see为你自己)你只需要将滚动事件绑定到你想要的任何元素,滚动将暂停类型

$( "yourelemntselector" ).scroll(function(){
    $(".type-o").typewriter('typepause');
});
相关问题