js 几个常用程序

来源:互联网 发布:linux配置java环境变量 编辑:程序博客网 时间:2024/05/17 22:46

图片随机出现:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style>
div{
position:absolute;}
</style>
<body>


<div id="imgs" width="130px" height="120px">
<img src="../../shixun练习/shixun/images/news_2.jpg"/>


</div>


</body>
<script language="javascript">
var img=document.getElementById("imgs");
var yan=true;
var w=document.documentElement.clientWidth-130,h=document.documentElement.clientHeight-120;
function suiji(){
if(q<0 || q>w) {
if(e<0 || e>h){
yan=false;
}
}else{
var q=parseInt(Math.random()*500);
var e=parseInt(Math.random()*1000);
img.style.top=q+"px";
img.style.left=e+"px";
}
setTimeout("suiji()","1000");
}


suiji();

</script>
</html>



倒计时


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
 
var maxtime = 60*60 //一个小时,按秒计算,自己调整!  
function CountDown(){   
if(maxtime>=0){   
minutes = Math.floor(maxtime/60);   
seconds = Math.floor(maxtime%60);   
msg = "距离结束还有"+minutes+"分"+seconds+"秒";  
document.getElementById("timer").innerHTML=msg;      
if(maxtime == 5*60) alert('注意,还有5分钟!');   
--maxtime;   
}   
else{   
clearInterval(timer);   
alert("时间到,结束!");   
}   
}   
function start(){
timer=setInterval("CountDown()",1000);


}

</script>
<body>
<button onclick="start()">开始倒计时</button>
<div id="timer" style="color:red"></div>  
</body>
</html>



随机抽奖

<html>    
<title>随机抽奖程序</title>    
<head> 
<meta   http-equiv=Content-Type   content="text/html;   charset=gb2312">    
</head>    
<body> 
<SCRIPT   LANGUAGE="JavaScript">    
  var   alldata   =   "ASP,PHP,AJAX,ASP.NET,CSHARP,VB.NET,DELPHI,VB,VC,JS"; //抽奖数据,以逗号分隔    
 var   alldataarr   =   alldata.split(",") ;   
  var   num   =   alldataarr.length-1 ;   
  var   timer ;   
  function   change(){    
  document.getElementById("oknum").value   =   alldataarr[GetRnd(0,num)];    
  }    
     
  function   start(){    
  clearInterval(timer);    
  timer   =   setInterval('change()',46); //随机数据变换速度,越小变换的越快    
  }    
     
  function   ok(){    
  clearInterval(timer);    
  }    
     
  function   GetRnd(min,max){    
  return   parseInt(Math.random()*(max-min+1));    
  }    
  </SCRIPT>    
  <input   type="text"   id="oknum"   name="oknum"   value="">    
  <button   onclick="start()"   accesskey="s">开始抽奖(<U>S</U>)</button>      
  <button   onclick="ok()"   accesskey="o">停止(<U>O</U>)</button>    
  </body>    
  </html>

原创粉丝点击