2016-11-27 68 views
0

現在,我的Index.html中已經有了所有的打字機腳本 - 但是,當點擊我的分支時,onload函數不會自動加載;目前觸發腳本的方法是使用onclick播放按鈕。如何在角度部分上自動加載腳本

還有別的辦法嗎?

//This is script on Index.html 
 

 
<script> 
 
    function Typewriter(sSelector, nRate) { 
 

 
    function clean() { 
 
     clearInterval(nIntervId); 
 
     bTyping = false; 
 
     bStart = true; 
 
     oCurrent = null; 
 
     aSheets.length = nIdx = 0; 
 
    } 
 

 
    function scroll(oSheet, nPos, bEraseAndStop) { 
 
     if (!oSheet.hasOwnProperty("parts") || aMap.length < nPos) { 
 
     return true; 
 
     } 
 

 
     var oRel, bExit = false; 
 

 
     if (aMap.length === nPos) { 
 
     aMap.push(0); 
 
     } 
 

 
     while (aMap[nPos] < oSheet.parts.length) { 
 
     oRel = oSheet.parts[aMap[nPos]]; 
 

 
     scroll(oRel, nPos + 1, bEraseAndStop) ? aMap[nPos] ++ : bExit = true; 
 

 
     if (bEraseAndStop && (oRel.ref.nodeType - 1 | 1) === 3 && oRel.ref.nodeValue) { 
 
      bExit = true; 
 
      oCurrent = oRel.ref; 
 
      sPart = oCurrent.nodeValue; 
 
      oCurrent.nodeValue = ""; 
 
     } 
 

 
     oSheet.ref.appendChild(oRel.ref); 
 
     if (bExit) { 
 
      return false; 
 
     } 
 
     } 
 

 
     aMap.length--; 
 
     return true; 
 
    } 
 

 
    function typewrite() { 
 
     if (sPart.length === 0 && scroll(aSheets[nIdx], 0, true) && nIdx++ === aSheets.length - 1) { 
 
     clean(); 
 
     return; 
 
     } 
 

 
     oCurrent.nodeValue += sPart.charAt(0); 
 
     sPart = sPart.slice(1); 
 
    } 
 

 
    function Sheet(oNode) { 
 
     this.ref = oNode; 
 
     if (!oNode.hasChildNodes()) { 
 
     return; 
 
     } 
 
     this.parts = Array.prototype.slice.call(oNode.childNodes); 
 

 
     for (var nChild = 0; nChild < this.parts.length; nChild++) { 
 
     oNode.removeChild(this.parts[nChild]); 
 
     this.parts[nChild] = new Sheet(this.parts[nChild]); 
 
     } 
 
    } 
 

 
    var 
 
     nIntervId, oCurrent = null, 
 
     bTyping = false, 
 
     bStart = true, 
 
     nIdx = 0, 
 
     sPart = "", 
 
     aSheets = [], 
 
     aMap = []; 
 

 
    this.rate = nRate || 100; 
 

 
    this.play = function() { 
 
     if (bTyping) { 
 
     return; 
 
     } 
 
     if (bStart) { 
 
     var aItems = document.querySelectorAll(sSelector); 
 

 
     if (aItems.length === 0) { 
 
      return; 
 
     } 
 
     for (var nItem = 0; nItem < aItems.length; nItem++) { 
 
      aSheets.push(new Sheet(aItems[nItem])); 
 
      /* Uncomment the following line if you have previously hidden your elements via CSS: */ 
 
      // aItems[nItem].style.visibility = "visible"; 
 
     } 
 

 
     bStart = false; 
 
     } 
 

 
     nIntervId = setInterval(typewrite, this.rate); 
 
     bTyping = true; 
 
    }; 
 

 
    this.pause = function() { 
 
     clearInterval(nIntervId); 
 
     bTyping = false; 
 
    }; 
 

 
    this.terminate = function() { 
 
     oCurrent.nodeValue += sPart; 
 
     sPart = ""; 
 
     for (nIdx; nIdx < aSheets.length; scroll(aSheets[nIdx++], 0, false)); 
 
     clean(); 
 
    }; 
 
    } 
 

 
/* usage: */ 
 
var oTWExample1 = new Typewriter(/* elements: */ "#homecopy, #bluebox, #first, #second, #third, #fourth, #fifth", /* frame rate (optional): */ 100); 
 
/* default frame rate is 100: */ 
 
// var oTWExample2 = new Typewriter("#controls"); 
 
// var oTWExample3 = new Typewriter(/* elements: */ "#second", /* frame rate (optional): */ 100); 
 

 
onload = function() { 
 
    oTWExample1.play(); 
 
    // oTWExample2.play(); 
 
}; 
 

 
function enter2(ele) { 
 
    if (event.keyCode == 13) { 
 
    document.getElementById("second").style.display = 'block'; 
 
    } 
 
}; 
 

 
function enter3(ele) { 
 
    if (event.keyCode == 13) { 
 
    document.getElementById("third").style.display = 'block'; 
 
    } 
 
}; 
 

 
function enter4(ele) { 
 
    if (event.keyCode == 13) { 
 
    document.getElementById("fourth").style.display = 'block'; 
 
    } 
 
}; 
 

 
function enter5(ele) { 
 
    if (event.keyCode == 13) { 
 
    document.getElementById("fifth").style.display = 'block'; 
 
    } 
 
}; < /script>
//This is a partial - I'd like it to automatically load the index.html script when the user is on the page - somehow only the onlock in the "controls" triggers the script 
 

 
    <p id="controls" style="text-align: center;">[&nbsp;<span class="intLink" onclick="oTWExample1.play();">Play</span> | <span class="intLink" onclick="oTWExample1.pause();">Pause</span> | <span class="intLink" onclick="oTWExample1.terminate();">Terminate</span>&nbsp;]</p> 
 

 
    <div id="poeminput" > 
 
    <div id="first" > 
 
    $he went to the <form><input type="text" onkeydown="enter2(this)"/></form> 
 
    </div> 
 
    </div> 
 

 
<input action="action" type="button" id="back" value="Back" onclick="history.go(-1);" />

+0

如何爲部分正在裝載進入頁面?誠實地說,目前這些看起來都不像角色。如果您希望在加載新的* view *時執行代碼,則可以從視圖的控制器執行此操作。否則,您可能需要從任何代碼調用您的腳本導致您的部分被加載。確實很難說出你想要完成的工作,因爲這不是一個角函數,在這裏你沒有角度的HTML,而且你正在使用'onclick','onkeydown'等等,它們沒有角度感知。沒有[mcve],這只是一個猜測。 – Claies

回答

0

就試試這個:

//這是劇本上的Index.html

<script> 

    onload(); // you need to actually call the function! 

    function Typewriter(sSelector, nRate) { 

    function clean() { 
     clearInterval(nIntervId); 
     bTyping = false; 
     bStart = true; 
     oCurrent = null; 
     aSheets.length = nIdx = 0; 
    } 

//....content omitted for breviety 

onload = function() { 
    oTWExample1.play(); 
    // oTWExample2.play(); 
}; 

//...content omitted 

function enter5(ele) { 
    if (event.keyCode == 13) { 
    document.getElementById("fifth").style.display = 'block'; 
    } 
}; 

</SCRIPT>

+0

似乎不工作 - 謝謝,雖然! – user7214732