編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。 【實例名稱】 JS代碼實現幻燈片式的導航菜單 【實例描述】 導航菜單用一個div封裝,每次只顯示一個菜單,并實現菜單變化時的幻燈片效果。 【實例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標題頁-本站(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">
//默認速度3秒,可以修改速度快慢。
var speed=3000
var news=new Array()
news[0]="<a href='index1.htm'>我的連接1</a>"//創建鏈接樹數組
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>" //顯示的信息內容按照格式添加。
i=0
if (document.all)
tickerobject=document.all.subtickertape.style
else
tickerobject=document.linkView.document
function regenerate(){
window.location.reload()
//重新加載頁面
}
function regenerate2(){
if (document.layers)
//非IE瀏覽器時的定時器
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]
//動態顯示鏈接(使用數組索引找到要顯示的鏈接)
if (i<news.length-1)
// 判斷是否已經顯示完所有鏈接
i++
//如果不是,則顯示下個鏈接
else
i=0 //否則從頭開始
setTimeout("update()",speed)
} function BgFade(red1, grn1, blu1, red2,
grn2, blu2, steps) {
sred = red1; sgrn = grn1; sblu = blu1;
//顏色的設置
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
//背景色的漸變效果,注意顏色的設置
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);
//循環執行漸變效果
}
step++;
}
</script>
</body>
</html>
【運行效果】  【難點剖析】 本例的重點是鏈接的顯示和特效的實現。所有鏈接都保存在“news”數組中。通過循環使用數組索引逐個顯示鏈接的內容,然后使用“RunFader”方法,實現鏈接更改時的幻燈片效果。 【源碼下載】 為了JS代碼的準確性,請點擊:幻燈片式的導航菜單 進行本實例源碼下載
使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。 |