简体   繁体   English

typed.js在第二次尝试中不起作用

[英]typed.js doesn't work for the second attempt

The first attempt to hover div.logo finish successfully and typed.js types the sentence. 悬停div.logo的第一次尝试成功完成,并且typed.js输入了该句子。 After that, when hover again on div.logo it doesn't work and doesn't type anything. 之后,当再次在div.logo上悬停时,它将不起作用,也不会键入任何内容。

    <script type="text/javascript">
    $( ".logo" ).hover(function() {
        $(this).toggleClass("clicked");
        if ($('.logo').hasClass('clicked')){
              $(function(){
                $('.logo').css('background-position','-20vmin center');
                console.log("n");
                  $(".logo h3").typed({
                    strings: ["Phoenix ^250 Programming ^250 Team"],
                    typeSpeed: 75
                  });
              });
        }
        else{
            $('.logo').find( "h3" ).text("");
            $('.logo span').remove();
            $('.logo').css('background-position','center left+1.5vmin');
        }
    });
    </script>

As you can see in the source code for typed.js , the function typed() assigns some data to the target element, and refuses to run if such data is set: 如您在typed.js源代码中看到的那样 ,函数typed()将一些数据分配给目标元素,如果设置了此类数据,则拒绝运行:

var $this = $(this),
    data = $this.data('typed'), // <<<
    options = typeof option == 'object' && option;
if (!data) // <<<
    $this.data('typed', (data = new Typed(this, options)));

Therefore you have to unset it before calling typed() twice: 因此,您必须在两次调用typed()之前取消设置它:

$('.logo h3')
    .data('typed', null) // <<<
    .typed({
        strings: ["Phoenix ^250 Programming ^250 Team"],
        typeSpeed: 75
    });

I noticed that Andrea's answer while worked in your case, it would mess the effect (make it too jagged) when used with a string array with multiple elements. 我注意到,Andrea的答案在您的情况下有效,当与具有多个元素的字符串数组一起使用时,会弄乱效果(使其参差不齐)。

If someone faces the same problem as I did, they probably could do what I did. 如果有人遇到与我相同的问题,他们可能会做我所做的事情。

I used a counter to check if typed.js is called for second time and re-added .logo h3 when done so. 我使用了一个计数器来检查typed.js是否第二次被调用,并在完成后重新添加.logo h3。

So, I added a wrapper. 因此,我添加了一个包装器。

<div class="wrapper">
<div class="logo"><h3></h3></div>
<div>

and then implemented 然后实施

 if(counter==1){
 $(".logo h3").typed({
        strings: stringArrayWithManyElements,
        typeSpeed: 75
      });
}
else{
    $('wrapper').html('');
    $(".logo h3")
    .typed({
        strings: stringArrayWithManyElements,
        typeSpeed: 75
      });
}

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

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