繁体   English   中英

Javascript,暂停打字机

[英]Javascript, pausing a typewriter

我的JavaScript有问题。 我面临的问题是我的一页网站。 我有一个在div中运行的typewriter.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一页

//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;
};

我现在要拥有的是,如果您离开内容。打字机继续,但是当您滚动回到内容时,打字机又重新启动,就像您在单词中启用了插入一样。 而且我目前想法不对,所以这就是为什么我需要一些帮助。

谢谢!

在更正代码后更新请参见您的自身),您只需要将scroll事件绑定到所需的任何元素,滚动就会暂停该类型

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM