简体   繁体   English

Javascript,暂停打字机

[英]Javascript, pausing a typewriter

I have a question with my javascript. 我的JavaScript有问题。 The problem I am facing is that, with my one page website. 我面临的问题是我的一页网站。 I have a typewriter.js that is running within a div. 我有一个在div中运行的typewriter.js。 What I am trying to accomplish is that when you are scrolling up or down, the typewriter 'freezes' (pause) and when you scroll back to the page, the typewriter continues again. 我要完成的工作是,当您上下滚动时,打字机会“冻结”(暂停),而当您滚动回到页面时,打字机会再次继续。

HTML Typewriter 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 Javascript一页

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

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

Javascript Typewriter 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;
};

What I am having now, is that if you leave the content.. The typewriter continues, but when you scroll back to the content, the typewriter starts again, like when you are having insert enabled in word. 我现在要拥有的是,如果您离开内容。打字机继续,但是当您滚动回到内容时,打字机又重新启动,就像您在单词中启用了插入一样。 And I am currently out of idea's, so that is why I need some help. 而且我目前想法不对,所以这就是为什么我需要一些帮助。

Thanks! 谢谢!

UPDATED after correcting your code( see for your self ) you only have to bind the scroll event to the whatever element you want and the scrolling will pause the type 在更正代码后更新请参见您的自身),您只需要将scroll事件绑定到所需的任何元素,滚动就会暂停该类型

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

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

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