Javascript 如何在卷轴上逐字母加载文本
大家好,当我滚动到div时,我试图加载我的h2文本一次,h2的内容必须像打字机一样一个字母一个字母地加载。我试过用这种方法,但不起作用。你们知道吗Javascript 如何在卷轴上逐字母加载文本,javascript,jquery,html,css,Javascript,Jquery,Html,Css,大家好,当我滚动到div时,我试图加载我的h2文本一次,h2的内容必须像打字机一样一个字母一个字母地加载。我试过用这种方法,但不起作用。你们知道吗 $(window).scroll( function(){ $('.inner h2').each(function (i){ var header = $(this); var header_content = header.html();
$(window).scroll( function(){
$('.inner h2').each(function (i){
var header = $(this);
var header_content = header.html();
var content = new Array();
content[i] = header_content;
position = new Array();
position[i] = $(this).position().top;
console.log(content[i]+' pos '+position);
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( bottom_of_window > position[i] ){
var arrayTitle = content[i].split('');
var i = 0;
var interval = setInterval(function(){
if (i > arrayTitle.length)
{
header.html(content[i]); // wipe out the <span> tags
clearInterval(interval);
}
else
{
$('<span>')
.html(arrayTitle[i])
.appendTo(header)
.hide()
.fadeIn(50);
i++;
}
}, 50);
}
});
});
$(窗口)。滚动(函数(){
$('.inner h2')。每个(函数(i){
var头=$(这是);
var header_content=header.html();
var content=新数组();
内容[i]=标题内容;
位置=新数组();
位置[i]=$(此).position().top;
console.log(内容[i]+'pos'+位置);
var bottom_of_window=$(window.scrollTop()+$(window.height());
if(窗口底部\u>位置[i]){
var arrayttle=内容[i]。拆分(“”);
var i=0;
var interval=setInterval(函数(){
if(i>arrayttle.length)
{
html(content[i]);//清除标记
间隔时间;
}
其他的
{
$('')
.html(arrayTitle[i])
.appendTo(标题)
.hide()
.fadeIn(50);
i++;
}
}, 50);
}
});
});
以下是我的看法:
我将scroll事件处理程序放在each函数中。
这样,每个元素都有自己的“实例”变量和处理程序。
它需要更多的调整,但这可能会帮助你作为一个基础
这对我来说毫无意义:
我没有附加span,因为我没有从您的描述/代码中真正理解您正试图用它实现什么
如果这不是你想要的,那么至少我清理了一点:)
您有两个变量i,更改一个并删除setInterval@user3699634之外的header.html,我很高兴它有帮助:)
// I would have just left a comment if I could.