我有一個JavaScript在html頁面上創建動畫。在Meteor項目之外,動畫完美無缺,因爲它只是包含在html文件中的.js文件。我怎樣才能讓它在Meteor上工作? JavaScript文件似乎根本不運行(它沒有做任何事情)。流星可以包含原始的javscript文件嗎?如果是的話如何?該文件是純JavaScript,沒有助手或任何定義Meteor結構的文件。如何在流星應用程序中包含原始JavaScript代碼
missionPage.js
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload= function() {
console.log('I m here');
var elements = document.getElementsByClassName('txt-rotate');
console.log(elements);
for (var i=0; i<elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #666
}";
document.body.appendChild(css);
};
missionPage.html
<template name="missionPage">
<div class="ui center aligned container">
<h3>Our story</h3>
<h1>This website is
<span
class="txt-rotate"
data-period="2000"
data-rotate='[ "not just the website", "simple.", "pure JS.",
"pretty.", "fun!" ]'></span>
</h1>
</div>
</template>
非常感謝! @chazsolo – Roberto
我有一個問題。你在這裏初始化txtRotate是什麼意思?我到底要寫什麼? @chazsolo – Roberto
這就是你最初編寫的'window.onload'函數或類似的東西。 – chazsolo