简体   繁体   English

Typed.js-如何多次调用onClick函数

[英]Typed.js - how to call function onClick multiple times

I'm using typed.js . 我正在使用typed.js

I want to run a function when a button is clicked. 我想在单击按钮时运行一个功能。 But I can only get the typed function to work once on the first click. 但是我只能让typed函数在第一次单击时工作一次。

$(function(){
    var options = {
        strings: ["First sentence.", "Second sentence."],
        typeSpeed: 0
    }
    $("button").click(function(){
        $(".element").typed(options);
    });
});

See this fiddle for example: http://jsfiddle.net/mattboldt/tcRUG/ 例如,请参见此小提琴: http : //jsfiddle.net/mattboldt/tcRUG/

Type.js is not giving any way to do so. Type.js没有提供任何方式。 I tried using its api but it is using seTimeout everywhere and behaves asynchronously and not giving any promise/deferred object. 我尝试使用其api,但它在所有地方都使用seTimeout,并且行为异步且不提供任何promise / deferred对象。 To get it work one solution is to remove the element and create again. 要使其正常工作,一种解决方案是删除元素并再次创建。 Personally i dont like this solution, looks hacky but it gets the job done 我个人不喜欢这种解决方案,看起来很笨拙,但是可以完成工作

$(function(){
    var options = {
        strings: ["First sentence.", "Second sentence."],
        typeSpeed: 0,
        callback: function(){
            var x = $(".element").text();
            $("#maindiv").html("");
            $("#maindiv").append('<span class="element">'+x+'</span>');
        }
    }
    $("button").click(function(){ 
        $(".element").text('');
       $(".element").typed(options);        
    });
}); 

in HTML 在HTML中

<div id="maindiv"><span class="element"></span></div>
<button>Type!</button>

I recently had a similar problem. 我最近有一个类似的问题。 I implemented the answer but I also wanted the previous string to 'stick'. 我实现了答案,但我也希望前面的字符串“粘住”。

For example, onclick I wanted typed to backspace the previous string before typing the new string. 例如,我要键入的onclick会在键入新字符串之前退格上一个字符串。

I added this to init of typed 我将此添加到类型的init

// Set the default string
if(self.isDefaultString) {
    self.strPos = self.strings[self.sequence[self.arrayPos]].length;

    if (self.isInput) {
        self.el.val(self.strings[self.sequence[self.arrayPos]]);
    } else if (self.contentType === 'html') {
        self.el.html(self.strings[self.sequence[self.arrayPos]]);
    } else {
        self.el.text(self.strings[self.sequence[self.arrayPos]]);
    }
}

and this to when the typed object is initiated 这到类型对象被初始化时

// default string of text
this.isDefaultString = this.options.isDefaultString;

then implemented typed like this 然后实现这样的输入

$("#contact header b").typed({
     strings: ["s1", "s2"],
     isDefaultString: true,
     typeSpeed: 30,
     startDelay: 250,
     showCursor: false
});

The result is that I would start with the string "s2" on the screen then typed would delete it and type "s1". 结果是,我将在屏幕上以字符串“ s2”开头,然后键入将删除它并键入“ s1”。

This is useful because if you want to call typed later on click. 这很有用,因为如果您想在以后单击时调用键入。 You can set the default to be "s2". 您可以将默认设置为“ s2”。 Then typed will delete "s2" and print "s3". 然后键入将删除“ s2”并打印“ s3”。

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

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