2017-06-29 24 views
1

我有一个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> 

回答

1

你去过阅读Guide to Meteor, specifically the section on app structure?如果没有,我会从那里开始。

回答您的问题:

灿流星包括原始JavaScript文件?

是的。

如果是这样怎么样?

任何数量的方式。您可以直接导入:

import './txtRotate.js'; 

或者你可以把它放在client/compatibility目录,它会被加载并先于其他JS文件执行。

最后,您missionPage.js文件应该只有MissionPage模板相关的代码在里面,而不是TxtRotate代码。移动TxtRotate到它自己的文件(不包括window.onload部分),将其导入内missionPage.js,并初始化它当模板呈现:

import { Template } from 'meteor/templating'; 
import './txtRotate.js'; 

Template.missionPage.onRendered(function() { 
    this.$('.txt-rotate').each(function(i, element) { 
    var toRotate = $(element).data('rotate'); 
    var period = $(element).data('period'); 

    if (toRotate) { 
     new TxtRotate(this.get(0), JSON.parse(toRotate), period); 
    } 
    }); 
}); 

你是改变你的TxtRotate功能是一个reusable Blaze Component另一种选择而不是一个独立的功能。例如:

<template name="missionPage"> 
    <div class="ui center aligned container"> 
    <h3>Our story</h3> 
    <h1>This website is {{> TxtRotate period="2000" rotate=words}}</h1> 
    </div> 
</template> 

其中words将在模板帮助程序中定义。

+0

非常感谢! @chazsolo – Roberto

+0

我有一个问题。你在这里初始化txtRotate是什么意思?我到底要写什么? @chazsolo – Roberto

+0

这就是你最初编写的'window.onload'函数或类似的东西。 – chazsolo

相关问题