| 編程(Programming)是編定程序的中文簡(jiǎn)稱,就是讓計(jì)算機(jī)代碼解決某個(gè)問題,對(duì)某個(gè)計(jì)算體系規(guī)定一定的運(yùn)算方式,使計(jì)算體系按照該計(jì)算方式運(yùn)行,并最終得到相應(yīng)結(jié)果的過程。為了使計(jì)算機(jī)能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計(jì)算機(jī)能夠理解的形式告訴計(jì)算機(jī),使得計(jì)算機(jī)能夠根據(jù)人的指令一步一步去工作,完成某種特定的任務(wù)。這種人和計(jì)算體系之間交流的過程就是編程。 【實(shí)例名稱】 JS代碼實(shí)現(xiàn)幻燈片式的導(dǎo)航菜單 【實(shí)例描述】 導(dǎo)航菜單用一個(gè)div封裝,每次只顯示一個(gè)菜單,并實(shí)現(xiàn)菜單變化時(shí)的幻燈片效果。 【實(shí)例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標(biāo)題頁(yè)-本站(www.xue51.com)</title>
<style>
#linkView{
position:relative;
layer-background-color:black;
width:400;
height:12;
; font-family: "宋體"; font-size: 9pt}
#subtickertape{
background-color:black;
position:absolute;
border: 1px solid black;
width:400;
height:12;
; font-family: "宋體"; font-size: 9pt
}
.subtickertapefont{
font:bold 9pt "宋體";
text-decoration:none;
color:white;
}
.subtickertapefont a{
color:white;
text-decoration:none;
; font-family: "宋體"; font-size: 9pt}
</style>
</head>
<body bgcolor="#fef4d9" onload="if
(document.all||document.layers) {regenerate2();update()}">
<div id="linkView">
<div id="subtickertape" class="subtickertapefont">初始化...</div>
</div>
<script language="JavaScript">
//默認(rèn)速度3秒,可以修改速度快慢。
var speed=3000
var news=new Array()
news[0]="<a href='index1.htm'>我的連接1</a>"//創(chuàng)建鏈接樹數(shù)組
news[1]="<a href='index2.htm'>我的連接2</a>"
news[2]="<a href='index3.htm'>我的連接3</a>"
news[3]="<a href='index4.htm'>我的連接4</a>"
news[4]="<a href='index5.htm'>我的連接5</a>"
news[5]="<a href='index6.htm'>我的連接6</a>"
news[6]="<a href='index7.htm'>我的連接7</a>" //顯示的信息內(nèi)容按照格式添加。
i=0
if (document.all)
tickerobject=document.all.subtickertape.style
else
tickerobject=document.linkView.document
function regenerate(){
window.location.reload()
//重新加載頁(yè)面
}
function regenerate2(){
if (document.layers)
//非IE瀏覽器時(shí)的定時(shí)器
setTimeout("window.onresize=regenerate",450)
} function update(){
BgFade(0xff,0xff,0xff, 0x00,0x00,0x00,10);
if (document.layers){
document.linkView.document.subtickertape.document.write
('<span class="subtickertapefont">'+news[i]+'</span>')
document.linkView.document.subtickertape.document.close()
}
else
document.all.subtickertape.innerHTML=news[i]
//動(dòng)態(tài)顯示鏈接(使用數(shù)組索引找到要顯示的鏈接)
if (i<news.length-1)
// 判斷是否已經(jīng)顯示完所有鏈接
i++
//如果不是,則顯示下個(gè)鏈接
else
i=0 //否則從頭開始
setTimeout("update()",speed)
} function BgFade(red1, grn1, blu1, red2,
grn2, blu2, steps) {
sred = red1; sgrn = grn1; sblu = blu1;
//顏色的設(shè)置
ered = red2; egrn = grn2; eblu = blu2;
inc = steps;
step = 0;
RunFader();
}
function RunFader() {
var epct = step/inc;
var spct = 1 - epct;
if (document.layers)
tickerobject.bgColor =
Math.floor(sred * spct + ered *
epct)*256*256 +
Math.floor(sgrn * spct + egrn * epct)*256 +
Math.floor(sblu * spct + eblu * epct);
else
//背景色的漸變效果,注意顏色的設(shè)置
tickerobject.backgroundColor=
Math.floor(sred * spct + ered *
epct)*256*256 +
Math.floor(sgrn * spct + egrn * epct)*256 +
Math.floor(sblu * spct + eblu * epct);
if ( step < inc ) {
setTimeout('RunFader()',50);
//循環(huán)執(zhí)行漸變效果
}
step++;
}
</script>
</body>
</html>
【運(yùn)行效果】  【難點(diǎn)剖析】 本例的重點(diǎn)是鏈接的顯示和特效的實(shí)現(xiàn)。所有鏈接都保存在“news”數(shù)組中。通過循環(huán)使用數(shù)組索引逐個(gè)顯示鏈接的內(nèi)容,然后使用“RunFader”方法,實(shí)現(xiàn)鏈接更改時(shí)的幻燈片效果。 【源碼下載】 為了JS代碼的準(zhǔn)確性,請(qǐng)點(diǎn)擊:幻燈片式的導(dǎo)航菜單 進(jìn)行本實(shí)例源碼下載
使用編程語(yǔ)言寫的程序,由于每條指令都對(duì)應(yīng)計(jì)算機(jī)一個(gè)特定的基本動(dòng)作,所以程序占用內(nèi)存少、執(zhí)行效率高。 |