繁体   English   中英

在JavaScript代码中与其他JavaScript代码一起定义字体

[英]Defining font inside javascript code with other javascript code

我的HTML中有这个Javascript,我想用其他Javascript代码更改字体。

这是我使用的脚本,它按小时显示一个句子。

<SCRIPT LANGUAGE="JavaScript"> 
day = new Date() 
hr = day.getHours() 
if (hr == 1) document.write("You can also work at night!") 
if (hr == 2) document.write("Go on! You can still push it!") 
if (hr == 3) document.write("Go go go, do not give up now!") 
if (hr == 4) document.write("Are you tired? pussy!") 
if (hr == 5) document.write("It's after 5.. just go and work some more, sleeping is almost usless now") 
if (hr == 6) document.write("Goodmorning! You made it true the night. Now GO GO GO!") 
if (hr == 7) document.write("Goedemorgen, het is 7 uur geweest.") 
if (hr == 8) document.write("Stay awake!") 
if (hr == 9) document.write("It's after 9! School starts! GO GO GO") 
if (hr == 10) document.write("Take no koffiebrake.") 
if (hr == 11) document.write("Hurry! Morning is almost finnised!") 
if (hr == 12) document.write("Damn! half of the day is alreddy gone! You are not working fast enouth!") 
if (hr == 13) document.write("No no, if you are a real good student you don't take brakes!") 
if (hr == 14) document.write("The morning is alreddy over, time is tikking!.") 
if (hr == 15) document.write("Go and get your coffie, drink it behind your computer so you wont't lose anney time!") 
if (hr == 16) document.write("No 4 o'clock for you! You still have no musch work to do!") 
if (hr == 17) document.write("What do you want to eat? Think quiq, lose no time!") 
if (hr == 18) document.write("Eat faster!") 
if (hr == 19) document.write("And back to work! To much stuff to do for tomorrow morging.") 
if (hr == 20) document.write("Take a koffie end procide") 
if (hr == 21) document.write("Eavening is almost done, have you finnised enouth work?") 
if (hr == 22) document.write("Stay strong, you can still finisch some work. Keep you focus.") 
if (hr == 23) document.write("It's nog that late yet, you dont need 9 hours sleep you pussy!") 
if (hr == 0) document.write("Come on you can still do it, finish that last parts!") 
</SCRIPT>

然后,我希望此彩虹Javascript为结果设置样式:

<script type="text/javascript">

function toSpans(span) {
  var str=span.firstChild.data;
  var a=str.length;
  span.removeChild(span.firstChild);
  for(var i=0; i<a; i++) {
    var theSpan=document.createElement("SPAN");
    theSpan.appendChild(document.createTextNode(str.charAt(i)));
    span.appendChild(theSpan);
  }
}

function RainbowSpan(span, hue, deg, brt, spd, hspd) {
    this.deg=(deg==null?360:Math.abs(deg));
    this.hue=(hue==null?0:Math.abs(hue)%360);
    this.hspd=(hspd==null?3:Math.abs(hspd)%360);
    this.length=span.firstChild.data.length;
    this.span=span;
    this.speed=(spd==null?50:Math.abs(spd));
    this.hInc=this.deg/this.length;
    this.brt=(brt==null?255:Math.abs(brt)%256);
    this.timer=null;
    toSpans(span);
    this.moveRainbow();
}


RainbowSpan.prototype.moveRainbow = function() {
  if(this.hue>359) this.hue-=360;
  var color;
  var b=this.brt;
  var a=this.length;
  var h=this.hue;

  for(var i=0; i<a; i++) {

    if(h>359) h-=360;

    if(h<60) { color=Math.floor(((h)/60)*b); red=b;grn=color;blu=0; }
    else if(h<120) { color=Math.floor(((h-60)/60)*b); red=b-color;grn=b;blu=0; }
    else if(h<180) { color=Math.floor(((h-120)/60)*b); red=0;grn=b;blu=color; }
    else if(h<240) { color=Math.floor(((h-180)/60)*b); red=0;grn=b-color;blu=b; }
    else if(h<300) { color=Math.floor(((h-240)/60)*b); red=color;grn=0;blu=b; }
    else { color=Math.floor(((h-300)/60)*b); red=b;grn=0;blu=b-color; }

    h+=this.hInc;

    this.span.childNodes[i].style.color="rgb("+red+", "+grn+", "+blu+")";
  }
  this.hue+=this.hspd;
}

</script>



<script type="text/javascript">

var r1=document.getElementById("r1"); //get span to apply rainbow
var myRainbowSpan=new RainbowSpan(r1, 55, 0, 255, 50, 200); //apply static rainbow effect
myRainbowSpan.timer=window.setInterval("myRainbowSpan.moveRainbow()", myRainbowSpan.speed);

</script>

我不知道如何组合这些脚本。 谁能帮忙?

一种方法是重构原始代码,然后简单地调用更改

这段代码将在您网页上的targetElement中创建一些数据的预期范围,然后将结果传递给RainbowSpan构造函数

function setTextBasedOnHour( target ) {
  var el = document.getElementById( target );
  var span = document.createElement('span');
  span.data = getTextBasedOnHour (getHour());
  el.appendChild(span);
  return el;
}

像这样

new RainbowSpan(setTextBasedOnHour( "textBasedOnHour" ));

我添加了一个小片段来说明如何实现

 function setTextBasedOnHour( target ) { var el = document.getElementById( target ); var span = document.createElement('span'); span.data = getTextBasedOnHour (getHour()); el.appendChild(span); return el; } function getHour() { return (new Date()).getHours(); } function getTextBasedOnHour( hour ) { var texts = [ "Come on you can still do it, finish that last parts!", "You can also work at night!", "Go on! You can still push it!", "Go go go, do not give up now!", "Are you tired? pussy!", "It's after 5.. just go and work some more, sleeping is almost usless now", "Goodmorning! You made it true the night. Now GO GO GO!", "Goedemorgen, het is 7 uur geweest.", "Stay awake!", "It's after 9! School starts! GO GO GO", "Take no koffiebrake.", "Hurry! Morning is almost finnised!", "Damn! half of the day is alreddy gone! You are not working fast enouth!", "No no, if you are a real good student you don't take brakes!", "The morning is alreddy over, time is tikking!.", "Go and get your coffie, drink it behind your computer so you wont't lose anney time!", "No 4 o'clock for you! You still have no musch work to do!", "What do you want to eat? Think quiq, lose no time!", "Eat faster!", "And back to work! To much stuff to do for tomorrow morging.", "Take a koffie end procide", "Eavening is almost done, have you finnised enouth work?", "Stay strong, you can still finisch some work. Keep you focus.", "It's nog that late yet, you dont need 9 hours sleep you pussy!" ]; return texts[hour || 0]; } function toSpans(span) { var str=span.firstChild.data; var a=str.length; span.removeChild(span.firstChild); for(var i=0; i<a; i++) { var theSpan=document.createElement("SPAN"); theSpan.appendChild(document.createTextNode(str.charAt(i))); span.appendChild(theSpan); } } function RainbowSpan(span, hue, deg, brt, spd, hspd) { this.deg=(deg==null?360:Math.abs(deg)); this.hue=(hue==null?0:Math.abs(hue)%360); this.hspd=(hspd==null?3:Math.abs(hspd)%360); this.length=span.firstChild.data.length; this.span=span; this.speed=(spd==null?50:Math.abs(spd)); this.hInc=this.deg/this.length; this.brt=(brt==null?255:Math.abs(brt)%256); this.timer=null; toSpans(span); this.moveRainbow(); } RainbowSpan.prototype.moveRainbow = function() { if(this.hue>359) this.hue-=360; var color; var b=this.brt; var a=this.length; var h=this.hue; for(var i=0; i<a; i++) { if(h>359) h-=360; if(h<60) { color=Math.floor(((h)/60)*b); red=b;grn=color;blu=0; } else if(h<120) { color=Math.floor(((h-60)/60)*b); red=b-color;grn=b;blu=0; } else if(h<180) { color=Math.floor(((h-120)/60)*b); red=0;grn=b;blu=color; } else if(h<240) { color=Math.floor(((h-180)/60)*b); red=0;grn=b-color;blu=b; } else if(h<300) { color=Math.floor(((h-240)/60)*b); red=color;grn=0;blu=b; } else { color=Math.floor(((h-300)/60)*b); red=b;grn=0;blu=b-color; } h+=this.hInc; this.span.childNodes[i].style.color="rgb("+red+", "+grn+", "+blu+")"; } this.hue+=this.hspd; } new RainbowSpan(setTextBasedOnHour( "textBasedOnHour" )); 
 <div id="textBasedOnHour"></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM