2016-03-07 20 views
0

時,函數將執行我有這些代碼用於在textarea佔位符內進行打字腳本。它工作正常。但我需要執行typeIt函數當我滾動到窗體div時。 「當滾動到

var txt =」滾動到該部分時執行的功能「; var modified_txt =「」;

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 

回答

0

您需要爲滾動事件捕獲並分配事件處理程序。

的筆畫演示目的,我採取了一個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> 

jQuery的

$('#scrollDiv').scroll(function(){ 
alert('scrolling'); 
}); 
+0

謝謝薩蒂什。我對jquery沒有太多的知識。你能給我一個工作的例子嗎? –

+0

@JahidHasan請檢查已編輯的答案 –

0

您可以使用這種方式(例如用你的代碼庫):

<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> 
+0

我已經嘗試過但不執行。請參閱鏈接http://codepen.io/jahid-webdev/pen/pgggXy。我需要讓盯着打字時滾動到窗體div #Vnuuk –

+0

您的主演是什麼意思? – Vnuuk

+0

對不起,它開始n#Vnuuk –