教学项目二十二 JavaScript中图片特效网页设计
【教学内容】
讲解JavaScript中几种图片特效网页设计
【教学目的】
使学生学会设计网页的图片特效
【教学重点】
图片特效的处理思想
【教学难点】
理解图片特效的处理方法
【教学方式】
讨论式、案例分析式、练习式相结合
【教学参考】
1.<<JavaScript 入门与提高>> 杨浩著 清华大学出版社
2.<<Internet 网页工场>> Wittime工作室 重庆出版社
3.<<JavaScript从入门到精通>> 电脑报社出版
4.<<JavaScript编程起步>> 人民邮电出版社
【教学过程】
【新课】
案例设计:
通过不断刷新,六张图片可以交替出现
分析:
先复制1.jpg 、 2.jpg、 3.jpg、 4.jpg、 5.jpg、 6.jpg到站点主目录下,通过刷新获取当前时间的秒钟数的不同,显示不同的图片。
程序代码如下:
<html>
<body>
<script language="JavaScript">
var dt = new Date();
var hr = dt.getSeconds();
if (hr<=10)
{ hr=1;}
if((hr<=20)&&(hr>10))
{ hr=2;}
if ((hr<=30)&&(hr>20))
{ hr=3;}
if ((hr<=40)&&(hr>30))
{ hr=7;}
if ((hr<=50)&&(hr>40))
{ hr=8;}
if ((hr<=60)&&(hr>50))
{ hr=9;}
var msg="<IMG SRC="+hr+".jpg>";
document.write(msg);
</script>
</body>
</html>
案例设计:
设计网页中的落叶效果。
程序代码如下:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>
<body>
<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Begin
var no = 12; // number of hearts
var speed = 6; // smaller number moves the hearts faster
var heart = "69.gif";
var flag;
var ns4up = (document.layers) ? 1 : 0; // browser sniffer
var ie4up = (document.all) ? 1 : 0;
var dx, xp, yp; // coordinate and position variables
var am, stx, sty; // amplitude and step variables
var i, doc_width = 800, doc_height = 600;
if (ns4up) {
doc_width = self.innerWidth;
doc_height = self.innerHeight;
} else if (ie4up) {
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
dx = new Array();
xp = new Array();
yp = new Array();
amx = new Array();
amy = new Array();
stx = new Array();
sty = new Array();
flag = new Array();
for (i = 0; i < no; ++ i) {
dx[i] = 0; // set coordinate variables
xp[i] = Math.random()*(doc_width-30)+10; // set position variables
yp[i] = Math.random()*doc_height;
amy[i] = 12+ Math.random()*20; // set amplitude variables
amx[i] = 10+ Math.random()*40;
stx[i] = 0.02 + Math.random()/10; // set step variables
sty[i] = 0.7 + Math.random(); // set step variables
flag[i] = (Math.random()>0.5)?1:0;
if (ns4up) { // set layers
if (i == 0) {
document.write("<layer name=\"dot"+ i +"\" left=\"15\" ");
document.write("top=\"15\" visibility=\"show\"><img src=\"");
document.write(heart+ "\" border=\"0\"></layer>");
} else {
document.write("<layer name=\"dot"+ i +"\" left=\"15\" ");
document.write("top=\"15\" visibility=\"show\"><img src=\"");
document.write(heart+ "\" border=\"0\"></layer>");
}
} else
if (ie4up) {
if (i == 0) {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION: ");
document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
document.write("visible; TOP: 15px; LEFT: 15px;\"><img src=\"");
document.write(heart+ "\" border=\"0\"></div>");
} else {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION: ");
document.write("absolute; Z-INDEX: "+ i +"; VISIBILITY: ");
document.write("visible; TOP: 15px; LEFT: 15px;\"><img src=\"");
document.write(heart+ "\" border=\"0\"></div>");
}
}
}
function snowNS() { // Netscape main animation function
for (i = 0; i < no; ++ i) { // iterate for every dot
if (yp[i] > doc_height-50) {
xp[i] = 10+ Math.random()*(doc_width-amx[i]-30);
yp[i] = 0;
flag[i]=(Math.random()<0.5)?1:0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = self.innerWidth;
doc_height = self.innerHeight;
}
if (flag[i])
dx[i] += stx[i];
else
dx[i] -= stx[i];
if (Math.abs(dx[i]) > Math.PI) {
yp[i]+=Math.abs(amy[i]*dx[i]);
xp[i]+=amx[i]*dx[i];
dx[i]=0;
flag[i]=!flag[i];
}
document.layers["dot"+i].top = yp[i] + amy[i]*(Math.abs(Math.sin(dx[i])+dx[i]));
document.layers["dot"+i].left = xp[i] + amx[i]*dx[i];
}
setTimeout("snowNS()", speed);
}
function snowIE() { // IE main animation function
for (i = 0; i < no; ++ i) { // iterate for every dot
if (yp[i] > doc_height-50) {
xp[i] = 10+ Math.random()*(doc_width-amx[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
flag[i]=(Math.random()<0.5)?1:0;
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
if (flag[i])
dx[i] += stx[i];
else
dx[i] -= stx[i];
if (Math.abs(dx[i]) > Math.PI) {
yp[i]+=Math.abs(amy[i]*dx[i]);
xp[i]+=amx[i]*dx[i];
dx[i]=0;
flag[i]=!flag[i];
}
document.all["dot"+i].style.pixelTop = yp[i] + amy[i]*(Math.abs(Math.sin(dx[i])+dx[i]));
document.all["dot"+i].style.pixelLeft = xp[i] + amx[i]*dx[i];
}
setTimeout("snowIE()", speed);
}
if (ns4up) {
snowNS();
} else if (ie4up) {
snowIE();
}
// End -->
</script>
<table border="0" width="99%" >
<tr>
<td width="100%">
<p align="center"><font size="6" face="楷体_GB2312" color="#0000FF">网页设计在线教程</font></td>
</tr>
<tr>
<td width="100%">FLASH</td>
</tr>
<tr>
<td width="100%">
<p align="left">HTML</td>
</tr>
<tr>
<td width="100%">FRONTPAGE</td>
</tr>
<tr>
<td width="100%">Dreamweaver</td>
</tr>
<tr>
<td width="100%">CSS</td>
</tr>
<tr>
<td width="100%">Fireworks</td>
</tr>
<tr>
<td width="100%">Photoshop</td>
</tr>
</table><table border="0" width="94%">
<tr>
<td width="100%"></td>
<td width="100%"></td>
</tr>
<tr>
<td width="200%" colspan="2">
<p align="center"><font size="5" face="宋体">COPYRIGHT @2003</font><font size="5" face="楷体_GB2312">湖北职院计科系学生网</font></td>
</tr>
<tr>
<td width="200%" colspan="2">
<p align="center"><font size="5">Mailto: xgzyljj@163.net</font></td>
</tr>
</table>
<p align="center"> </p>
</body>
</html>
案例设计:
设计背景图片下滚,文字上滚的网页特效。
程序代码如下:
<html>
<head>
<bgsound src=liangzhu.mp3 loop=1>
<SCRIPT language=JavaScript>
//Author:redriver
var index =15
text = new Array(14);
text[0] =' 偶然 '
text[1] =''
text[2] =' 汪国真 '
text[3] =''
text[4] ='我是天空里的一片云,'
text[5] ='偶尔投影在你的波心——'
text[6] ='你不必诧异,'
text[7] ='更无须欢喜——'
text[8] ='在转瞬间消灭了踪影。'
text[9] =''
text[10] ='你我相逢在黑暗的海上,'
text[11] ='你有你的,我有我的,方向; '
text[12] ='你记得也好, '
text[13] ='最好你忘掉, '
text[14] ='在这交汇时互放的光亮! '
document.write ("<p align=center><marquee scrollamount='1' scrolldelay='60' direction= 'UP' width='550 ' id=xiaoqing height='200' onmouseover=xiaoqing.stop() onmouseout=xiaoqing.start()>");
for (i=0;i<index;i++){
document.write ("<B><font face=隶书 color=#00ee00 size=7>");
document.write (text[i] + "</font></B><br>");
}
document.write ("</marquee>");
documetn.write("</p>");
</SCRIPT>
</head>
<body background="8.jpg">
<script language="JavaScript">
<!-- Begin
var backgroundOffset = 0;
var bgObject = eval('document.body');
function scrollBG(maxSize) {
backgroundOffset = backgroundOffset + 1;
if (backgroundOffset > maxSize) backgroundOffset = 0;
bgObject.style.backgroundPosition = "0 " + backgroundOffset;
}
var ScrollTimer = window.setInterval("scrollBG(307)", 64);
// End -->
</script>
</body>
</html>
【课后小结】
本例中的图片特效程序比较有代表性,要求能够掌握通过修改给定的源代码,实现满足用户要求的特效网页。