开发者

How to make this jQuery function in to .click()

开发者 https://www.devze.com 2023-01-24 03:25 出处:网络
I don\'t get it, What I want to do is not when its loading do the typing, but when I click on a link it starts typing. How does this work?

I don't get it, What I want to do is not when its loading do the typing, but when I click on a link it starts typing. How does this work?

var text = "The quick fox jumped over the lazy dog.";
var charCount = text.length;
var currentLetterCount = 0;
var speed = 100; // How fast should it type?
var $input = $(".some-textbox");

function writeLetter() {
    var currentText = $input.val();
    var currentLetter = text.charAt(currentLetterCount);
    currentLetterCount++;
    $input.val(currentText + currentLetter);
    if(currentLetterCount == charCount)
        clearInterval(timerId);
}

var timerId = setInterval(writeLetter, speed);
开发者_C百科<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<input type="text" style="width: 500px" class="some-textbox" />

and jsfiddle: http://jsfiddle.net/7K3UE/


You just need to start the timer in a .click() handler instead of directly in the document.ready handler, like this:

$(".some-link").click(function() {
  timerId = setInterval(writeLetter, speed);
});

You can test it here.


You just need to put the trigger into an onclick event or similar:

function startTyping() {
var timerId = setInterval(writeLetter, speed);
}

<input type="button" value="Start Animation" onclick="startTyping();">A Link</a>

Is that close to what you are after?


You'll need to ( read about | look up | search for ) the onclick event; because you're using jQuery, you ought to look at the click event.


Update your code on this URL http://jsfiddle.net/7K3UE/12/

Added this:

$("#lnkClick").bind("click",function(){
    var timerId = setInterval(writeLetter, speed);
})
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号