Javascript脚本:scrollTo()方法实现滚动效果
scrollTo()方法:意思是滚动到指定坐标。
scrollBy()方法:意思是滚动到指定像素。
一篇介绍:scrollBy()方法
与scrollBy()意思上有些区别但直到的效果还是一样的。
看scrollTo()方法示例:
- <html>
- <head><title>使用scrollTo()方法滚动文档内容 /title>
- <script language="JavaScript">
- <!--
- winObj=window.open("http://www.cngothic.com","mypic",
- "width=250,height=192,resizable=no"); // 打开一个新窗口,显示一个包含图像的HTML页面
- winObj.moveTo(0, 0); //将新窗口移动到屏幕的左上角
- winObj.focus(); //让新窗口显示在其他窗口前面
- var pixelpos=0; //记录图像的当前显示位置(x坐标)
- var ImgWidth=640; //设置图像宽度
- var pixelstep = 1; //设置文档内容滚动速度
- var timeout;
- function startScroll(){
- if (pixelpos <= (ImgWidth - 250)){ //当文档内容未滚动到窗口边界时
- pixelpos += pixelstep;
- winObj.scrollTo(pixelpos,0); //将文档滚动到x方向新的位置
- }
- else{ //当文档内容滚动到窗口边界时,将当前图像显示位置置0,重新显示图像
- pixelpos=0;
- }
- timeout=setTimeout("startScroll()",2); //每隔20毫秒调用一次本函数
- }
- function stopHere(){
- clearTimeout(timeout); // 通过停止计时器来终止文档内容滚动
- }
- function closeWindow(){ //关闭新创建的窗口
- winObj.close();
- }
- // -->
- </script>
- </head>
- <body bgColor="lightgreen">
- <font face="宋体" size=4 >
- <b><br><center>
- 滚动新打开窗口的内容
- <form>
- <input type="button"
- value="开始滚动"
- onClick="startScroll();">
- <input type="button"
- value="停止滚动"
- onClick="stopHere();">
- </form>
- <font size=-1>
- <p>当希望关闭新打开窗口时,请单击下面的链接 <br>
- <a href="javascript:closeWindow()">关闭新打开窗口 </a></h3>
- </body>
- </html>
Javascript脚本, scrollBy(), scrollTo()
Javascript脚本:setTimeout()方法制作跑马灯
setTimeout()方法:此方法用于延迟N毫秒后调用X()函数.即setTimeout(x(),1000)
跑马灯特效的思路就是利用setTimeout()方法不断的调用x()函数让其把灯滚动起来.
看下面示例:
- <script language="JavaScript">
- <!--
- var today = new Date();
- var year=today.getFullYear()+1; //得到下一年的年份
- var newYear = new Date(year,1,1);
- var diff = newYear.getTime() - today.getTime();//计算新一年元旦与今天相差的毫秒数
- var days =Math.floor(diff / (1000 * 60 * 60 * 24 ));// 将相差的毫秒数转换为天数
- var str="离"+year+"年元旦还有 " + days + " 天!!!";
- function scroller(){
- str = str.substring(1, str.length) + str.substring(0,1);
- document.title=str;
- window.status=str;
- setTimeout("scroller()", 400); // 每隔400毫秒调用一次scroller()函数
- }
- scroller();
- // -->
- </script>
Javascript脚本, setTimeout(), 跑马灯
Javascript脚本:Function 对象的定义和使用
利用函数对象(Function对象),可以像定义普通对象那样动态地定义函数.
创建函数的语法:var myFunction = new Function(arg1,…,agrN, body)
argN为创建函数的参数, body为函数的主体.看下面示例:
- <script language="JavaScript">
- <!--
- var sumUp = new Function("x", "y", "z", "return x + y + z; ");
- //三个参数.x, y, z. 函数主体 return x+y+z 即函数返回的值赋值于所创建的对象
- document.write(sumUp.length + "<br />")
- //length返回创建sumUp对象函数参数的个数
- window.onload = new Function ( "document.bgColor='pink';");
- document.write( "sumUp(15,20)的结果为:" + sumUp(15,20,30)+ "<br>");
- document.write( "sumUp()的形式参数个数为:" + sumUp.length+ "<br>");
- document.write( "sumUp()的toString()方法执行结果为:" + sumUp.toString()+ "<br>");
- //-->
- </script>
Function, Javascript脚本, 对象
Javascript脚本:随机数-Math对象的random()方法
javascript对象Math中的random()方法所产生的随机数是:0-1之间的小数。与vbscript还是有很大差别的。
随机数为什么那怎么利用尼。如果你想用到2位正整数。可以Math.random() * 100 扩大一百倍这样就是两数正整数。然后再用截断函数把随机数取整。要想获取两位负整数前面加个-自己导弄吧。
实例:
- var Num1 = Math.floor(Math.random() * 100);
- document.write(Num1 + "<br />");
Javascript脚本, Math对象, random()方法, 随机数