JavaScript基础之第十二天js效果实战

  1.  定时器

    1. 两种定时器
      1. <!DOCTYPE html>
        <html>
        	<head>
        		<meta charset="utf-8">
        		<title></title>
        	</head>
        	<body>
        	</body>
        	<!-- js代码 -->
        	<script type="text/javascript">
        		/* 
        			1.setInterval(要执行的动作,时间:毫秒);//多少时间就执行,重复执行多次
        			2.setTimeout(要执行动作,时间:毫秒)//多少时间后执行,只执行依次 
        		*/
        	   var p1=setInterval(function(){document.write("这是重复执行的一定时间的定时器(setInterval)"+"<br/>")},4000);
        	   var p2=setTimeout(function(){alert("这是经过一段时间的定时器(setTimeout())")},2000);
        	</script>
        </html>
        
    2. 清理两种定时器
      1. <!DOCTYPE html>
        <html>
        	<head>
        		<meta charset="utf-8">
        		<title></title>
        	</head>
        	<body>
        		<button id="btn">点击</button>
        	</body>
        	<!-- js代码 -->
        	<script type="text/javascript">
        		/* 
        			1.setInterval(要执行的动作,时间:毫秒);//多少时间就执行,重复执行多次
        			2.setTimeout(要执行动作,时间:毫秒)//多少时间后执行,只执行依次 
        		*/
        	   var p1=setInterval(function(){document.write("这是重复执行的一定时间的定时器(setInterval)"+"<br/>")},4000);
        	   var p2=setTimeout(function(){alert("这是经过一段时间的定时器(setTimeout())")},2000);
        	   // 清理定时器
        	   /* 
        		   1.clearInterval(要清理的定时器名称)
        		   2.clearTimeout(要清理定时器名称) 
        		*/
        	   // 注意:按钮一定要在定时器出现前点击
        	   var btn=document.getElementById("btn");
        	   btn.onclick=function()
        	   {
        		   clearTimeout(p2);
        		   clearInterval(p1);
        	   }
        	</script>
        </html>
        

         

    3. 定时器三种写法
      1. <!DOCTYPE html>
        <html>
        	<head>
        		<meta charset="utf-8">
        		<title></title>
        	</head>
        	<body>
        		<button id="btn">点击</button>
        	</body>
        	<!-- js代码 -->
        	<script type="text/javascript">
        		/* 
        			1.setInterval(要执行的动作,时间:毫秒);//多少时间就执行,重复执行多次
        			2.setTimeout(要执行动作,时间:毫秒)//多少时间后执行,只执行依次 
        		*/
        	   // document.write("这是去除定时器前");
        	   // var p1=setInterval(function(){document.write("这是重复执行的一定时间的定时器(setInterval)"+"<br/>")},4000);
        	   // var p2=setTimeout(function(){alert("这是经过一段时间的定时器(setTimeout())")},2000);
        	   
        	   // 清理定时器
        	   /* 
        		   1.clearInterval(要清理的定时器名称)
        		   2.clearTimeout(要清理定时器名称) 
        		*/
        	   // 注意:按钮一定要在定时器出现前点击
        	   var btn=document.getElementById("btn");
        	   btn.onclick=function()
        	   {
        		   clearTimeout(p2);
        		   clearInterval(p1);
        	   }
        	  function abc()
        	  {
        	  		   alert("这是有函数名,不能有参数形式!");
        	  } 
        	  function abcd(a)
        	   {
        		   alert(a);
        	   }
        	   // 1.匿名函数:
        	   // setInterval(function(){document.write("这是匿名函数形式!");},2000)
        	   // 2.有函数名,不能有参数
        	   // setInterval(abc,2000);
        	   // 3.把函数当成字符串,就可以传参数
        	   var a="函数当成字符串形式";
        	   setInterval("abcd(a)",2000);
        	</script>
        </html>
        

         

    4. 倒计时跳转效果
      1. <!DOCTYPE html>
        <html>
        	<head>
        		<meta charset="utf-8">
        		<title></title>
        		<!-- css代码 -->
        		<style type="text/css">
        			*
        			{
        				padding: 0;
        				margin: 0px;
        			}
        			.box
        			{
        				width: 400px;
        				height: 50px;
        				margin:0 auto;
        				text-align: center;
        				font-size: 44px;
        			}
        		</style>
        	</head>
        	<body>
        		<!-- <button id="btn">点击</button> -->
        		<div class="box">
        			剩余跳转时间:<span id="clock">10</span>秒
        			</div>
        	</body>
        	<!-- js代码 -->
        	<script type="text/javascript">		
        	   var clock=document.getElementById("clock");//获取span
        	   var t=clock.innerHTML;//获取秒数
        	   function newsFun()
        	   {
        		   t--;
        		   clock.innerHTML=t;
        		   if(t<=0)
        		   {
        			   clearInterval(timer);
        			   // 设置或者返回当前页面的URL
        			   location.href="https://www.csdn.net";
        		   };
        	   }
        	   var timer=setInterval(newsFun,1000);
        	</script>
        </html>
        

    5. 运动效果
      1. <!DOCTYPE html>
        <html>
        	<head>
        		<meta charset="UTF-8">
        		<title></title>
        		<style type="text/css">
        			*{margin: 0;padding: 0;}
        			body{position: relative;}
        			#zxw{
        				width: 50px;height: 50px;background: green;
        				position: absolute;top: 100px;
        			}
        		</style>
        	</head>
        	<body>
        		<button type="button" id="btn">点我移动</button>
        		<div id="zxw" style="left: 0px;"></div>
        		<script type="text/javascript">
        			var btn = document.getElementById("btn");
        			var zxw = document.getElementById("zxw");
        			var buCang = 10;
        			btn.onclick = function(){				
        				var timer = setInterval(function(){					
        					//获取zxw的left值,转成整数
        					var o_left = parseInt(zxw.style.left);
        					var n_left = o_left+buCang;
        					zxw.style.left = n_left+"px";
        					if ( n_left>400) {
        						buCang = -10;
        					}else if(n_left==0){
        						buCang = 10;
        					};	
        				},200);				
        			};			
        		</script>
        	</body>
        </html>
        

         

    6.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值