用JavaScript来控制状态栏
1、跳动的文字
把以下代码放在<head></head>之间:
<script language="JavaScript">
<!-- Hide
var speed = 10
var pause = 1500
var timerID = null
var bannerRunning = false
var ar = new Array()
ar[0] = "MESSAGE #1"
ar[1] = "MESSAGE #2"
ar[2] = "MESSAGE #3"
ar[3] = "MESSAGE #4"
var message = 0
var state = ""
clearState()
function stopBanner() {
if (bannerRunning)
clearTimeout(timerID)
bannerRunning = false
}
function startBanner() {
stopBanner()
showBanner()
}
function clearState() {
state = ""
for (var i = 0; i < ar[message].length; ++i) {
state += "0"
}
}
function showBanner() {
if (getString()) {
message++
if (ar.length <= message)
message = 0
clearState()
timerID = setTimeout("showBanner()", pause)
bannerRunning = true
} else {
var str = ""
for (var j = 0; j < state.length; ++j) {
str += (state.charAt(j) == "1") ? ar[message].charAt(j) : " "
}
window.status = str
timerID = setTimeout("showBanner()", speed)
bannerRunning = true
}
}
function getString() {
var full = true
for (var j = 0; j < state.length; ++j) {
if (state.charAt(j) == 0)
full = false
}
if (full)
return true
while (1) {
var num = getRandom(ar[message].length)
if (state.charAt(num) == "0")
break
}
state = state.substring(0, num) + "1" + state.substring(num + 1, state.length)
return false
}
function getRandom(max) {
return Math.round((max - 1) * Math.random())
}
// -->
</script>
把以下代码放在<body>中:
<body onLoad="startBanner()">
2、文本变换
把以下代码放在<head></head>之间:
<script language="JavaScript">
function update(msg) {
var pad_str="";
n = msg.length;
if(n<81) {
pad = (81-n)/2;
for(var i=0; i<pad; i++) {
pad_str+=" ";
}
}
CurrentMsg = pad_str + msg;
document.messages.field.value = CurrentMsg;
clearTimeout(timer);
timer = setTimeout("idleMsg()",5000);
}
function MakeArray(n) {
this.length=n;
for(var i = 1; i<= n; i++) {
this[i] = "";
}
return(this);
}
var index = 1;
var notice_num = 4;
var notices = new MakeArray(notice_num);
notices[1] = "MESSAGE #1";
notices[2] = "MESSAGE #2";
notices[3] = "MESSAGE #3";
notices[4] = "MESSAGE #4";
function idleMsg() {
update(notices[index++]);
if(index>notice_num) { index=1; }
}
</script>
把以下代码放在HTML文件任意地方:
<form name="messages" onSubmit="return false">
<input type="text" name="field" size=60 height=5 value="">
</form>