Javascript 当滚动到该节时,将执行函数
我有这些代码,可以在textarea占位符中创建一个键入脚本。它工作得很好。但是当我滚动到表单div时,我需要执行typeIt函数 var txt=“滚动到节时将执行函数”; var修改_txt=“”Javascript 当滚动到该节时,将执行函数,javascript,jquery,Javascript,Jquery,我有这些代码,可以在textarea占位符中创建一个键入脚本。它工作得很好。但是当我滚动到表单div时,我需要执行typeIt函数 var txt=“滚动到节时将执行函数”; var修改_txt=“” 您需要捕获并为scroll事件分配一个事件处理程序 为了演示的目的,我用一些虚拟数据做了一个div HTML内容 <div id="scrollDiv" style="width:50px;height:50px;overflow:scroll;" >We've Worked For
您需要捕获并为scroll事件分配一个事件处理程序 为了演示的目的,我用一些虚拟数据做了一个div HTML内容
<div id="scrollDiv" style="width:50px;height:50px;overflow:scroll;" >We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here
We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here</div>
您可以这样使用它(代码库示例):
1
1.
1.
1.
1.
2.
2.
2.
2.
var oTop=$('#2').offset().top-window.innerHeight;
$(窗口)。滚动(函数(){
var pTop=$('body').scrollTop();
console.log(pTop+'-'+oTop);//仅用于调试
if(pTop>oTop){
警惕();
}
});
功能人性化(){
返回Math.round(Math.random()*(200-30))+30;
}
//删除修改后字符串中的最后一个字符
函数删除字符(文本){
//返回除最后一个字符以外的所有字符
text=text.substring(0,text.length-1);
返回文本;
}
//插入文字末尾添加的字符
函数addCharacter(文本,添加字符){
text=文本+添加的字符;
返回文本;
}
//输入错误[char]。错误只是一个自我引用,未使用
变量类型={
}
var超时;
var txtLen=txt.length;
var-char=0;
$('textarea').attr('placeholder','|');
函数typeIt(){
修改的_txt+=txt.charAt(char);
$('textarea').attr('placeholder',modified_txt+'|');
if(char==txtLen){
$('textarea').attr('placeholder',$('textarea').attr('placeholder').slice(0,-1));//删除“|”
return;//文本完全写入后停止循环。
}
var测试=输入错误[char];
如果(测试!==未定义){
setTimeout(函数(){
var chunk_one=测试.校正(修改的_txt);
modified_txt=chunk_one;
char++;
键入它();
},人性化());
}
//如果没有发现拼写错误,则移动到下一个字符
否则{
setTimeout(函数(){
char++;
键入它();
},人性化());
}
}
$(函数(){
键入它();
});//结束jquery
谢谢萨蒂什。我对jquery不太了解。你能给我一个工作示例吗?我试过了,但没有执行。请查看链接。我需要在滚动到表单div时开始键入,你说的starring是什么意思?对不起,它没有开始,它正在启动n#vnuukw当你滚动到div时,它启动了你的函数?它启动了吗?不,它不执行这个功能
<div id="scrollDiv" style="width:50px;height:50px;overflow:scroll;" >We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here
We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here We've Worked For a lot of people and of course we've made them so happy! You can read some handpicked Word about us here</div>
$('#scrollDiv').scroll(function(){
alert('scrolling');
});
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
<div id="2">2</div>
<div>2</div>
<div>2</div>
<div>2</div>
<script>
var oTop = $('#2').offset().top - window.innerHeight;
$(window).scroll(function () {
var pTop = $('body').scrollTop();
console.log(pTop + ' - ' + oTop); //just for your debugging
if (pTop > oTop) {
alert();
}
});
function humanize() {
return Math.round(Math.random() * (200 - 30)) + 30;
}
//Delete final character in modified string
function deleteCharacter(text) {
//return everything but the last character
text = text.substring(0, text.length - 1);
return text;
}
//Insert character_added at end of text
function addCharacter(text, character_added) {
text = text + character_added;
return text;
}
//typos[char].error is just a self reference, it is not used
var typos = {
}
var timeOut;
var txtLen = txt.length;
var char = 0;
$('textarea').attr('placeholder', '|');
function typeIt() {
modified_txt += txt.charAt(char);
$('textarea').attr('placeholder', modified_txt + '|');
if (char === txtLen) {
$('textarea').attr('placeholder', $('textarea').attr('placeholder').slice(0, -1)); // remove the '|'
return; //Stop the loop once text is completely written.
}
var test = typos[char];
if (test !== undefined) {
setTimeout(function () {
var chunk_one = test.correction(modified_txt);
modified_txt = chunk_one;
char++;
typeIt();
}, humanize());
}
//If no typos are found then move to the next character
else {
setTimeout(function () {
char++;
typeIt();
}, humanize());
}
}
$(function () {
typeIt();
});//end jquery
</script>