jquery函数“.textAnim”什么都不做 textTyping_animation();工作正常,但.textAnim不起作用。 这是插件代码:
function textTyping_animation(text,location,speed){
var progress = 0;
var a = setInterval(function(){
progress++;
document.getElementById(location).innerHTML = text.substring(0,progress);
if(progress == text.length){
clearInterval(a);
}
},speed);
};
(function($){
$.fn.textAnim = function(text,speed){
var progress = 0;
var a = setInterval(function(){
progress++;
$(this).html(text.substring(0,progress));
if(progress == text.length){
clearInterval(a);
}
},speed);
};
})(jQuery);
这是执行:
$(document).ready(function(){
starttext = function()
{
document.getElementById("textbox").style.display = "block";
//textTyping_animation("Dit is een dummy text!","textbox_inner",70);
$("#textbox_inner").textAnim("Dit is een dummy text!",70);
}
});
Html加载代码:
<script type="text/javascript" language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script type="text/javascript" language="javascript" src="js/jqueryplugins.js" ></script>
<script type="text/javascript" language="javascript" src="js/libraries/gamelibrary.js"></script>
答案 0 :(得分:1)
问题是传递给this
的回调函数中的setInterval()
是窗口,而不是您希望它的元素。修改代码以在this
函数(确实引用正确的元素)中保存对textAnim()
的引用,然后在传递给setInterval()
的回调中使用它:
(function($){
$.fn.textAnim = function(text,speed){
var progress = 0,
element = this; // this will be the element in the jQuery object
var a = setInterval(function(){
progress++;
$(element).html(text.substring(0,progress));
if(progress == text.length){
clearInterval(a);
}
},speed);
};
})(jQuery);
这是一个updated DEMO。
答案 1 :(得分:0)
更改
$(document).ready(function(){
starttext = function()
{
document.getElementById("textbox").style.display = "block";
//textTyping_animation("Dit is een dummy text!","textbox_inner",70);
$("#textbox_inner").textAnim("Dit is een dummy text!",70);
}
});
是
$(document).ready(function(){
starttext = function()
{
document.getElementById("textbox").style.display = "block";
//textTyping_animation("Dit is een dummy text!","textbox_inner",70);
$("#textbox_inner").textAnim("Dit is een dummy text!",70);
}
starttext();
});
它应该有用;