国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

web前端之定時器的使用

2024-04-27 15:03:57
字體:
來源:轉載
供稿:網友

web前端之定時器的使用

定時器的簡單介紹:

這里寫圖片描述

獲取系統時間:

這里寫圖片描述

定時器的使用1:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>定時器</title><script type="text/javascript">function show(){ alert("a");}setInterval(show,1000);</script></head><body></body></html>

定時器的使用2:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>定時器的開啟和關閉</title><script type="text/Javascript">window.onload=function(){ var oBtn1=document.getElementById("btn1"); var oBtn2=document.getElementById("btn2"); oBtn1.onclick=function(){ timer=setInterval(function(){ alert('a'); },1000); }; oBtn2.onclick=function(){ clearInterval(timer); };};</script></head><body><input id="btn1" type="button" name="開啟" value="開啟"><input id="btn2" type="button" name="關閉" value="關閉"> </body></html>

開啟定時器有一個對應的參數,我們使用這個對應的參數,來開啟和關閉對應的定時器

定時器的運動基礎:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>運動基礎</title><style type="text/CSS">#div1{ width: 200px; height: 200px; background: red; position: absolute; left: 0; top: 50px;}</style><script type="text/javascript">setInterval(function(){ var oDiv=document.getElementById("div1"); oDiv.style.left=oDiv.offsetLeft+10+'px';},30);</script></head><body> <div id="div1"></div></body></html>

js中有一個offsetLeft/offsetTop/offsetWidth/offsetHight可以綜合考慮影響這個模塊位置的屬性

移動相冊:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>圖片滾動</title><style type="text/css">*{ margin: 0; padding: 0;}#div1{ width: 716px; height: 119px; margin: 100px auto; position: relative; background: red;}#div1 ul{ position: absolute; left: 0px; top: 0px;}#div1 ul li{ float: left; width: 179px; height: 119px; list-style: none;}</style><script type="text/javascript">window.onload=function(){ var oDiv=document.getElementById("div1"); var oUl=oDiv.getElementsByTagName("ul")[0]; setInterval(function(){ oUl.style.left=oUl.offsetLeft-2+"px"; },30);};</script></head><body> <div id="div1"> <ul> <li><img src="images/1.jpg"></li> <li><img src="images/2.jpg"></li> <li><img src="images/3.jpg"></li> <li><img src="images/4.jpg"></li> </ul> </div></body></html>

移動相冊完整版:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>圖片滾動</title><style type="text/css">*{ margin: 0; padding: 0;}#div1{ width: 716px; height: 119px; margin: 100px auto; position: relative; background: red; overflow: hidden;}#div1 ul{ position: absolute; left: 0px; top: 0px;}#div1 ul li{ float: left; width: 179px; height: 119px; list-style: none;}</style><script type="text/javascript">window.onload=function(){ var oDiv=document.getElementById("div1"); var oUl=oDiv.getElementsByTagName("ul")[0]; var aLi=oUl.getElementsByTagName("li"); var speed=2; oUl.innerHTML=oUl.innerHTML+oUl.innerHTML; oUl.style.width=aLi[0].offsetWidth*aLi.length+'px'; function move(){ if(oUl.offsetLeft<-oUl.offsetWidth/2){ oUl.style.left='0'; } if(oUl.offsetLeft>0){ oUl.style.left=-oUl.offsetWidth/2+"px"; } oUl.style.left=oUl.offsetLeft+speed+"px"; } var timer=setInterval(move,30); oDiv.onmouSEOver=function(){ clearInterval(timer); }; oDiv.onmouseout=function(){ timer=setInterval(move,30); }; document.getElementsByTagName('a')[0].onclick=function(){ speed=-2; }; document.getElementsByTagName('a')[1].onclick=function(){ speed=2; };};</script></head><body> <a href="javascript:;">向左走</a> <a href="javascript:;">向右走</a> <div id="div1"> <ul> <li><img src="images/1.jpg"></li> <li><img src="images/2.jpg"></li> <li><img src="images/3.jpg"></li> <li><img src="images/4.jpg"></li> </ul> </div></body></html>

這邊的向左走向右走可以控制其滾動的方向


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 包头市| 泾川县| 孝昌县| 富宁县| 汝阳县| 松桃| 五原县| 开平市| 金坛市| 富阳市| 巴东县| 巨鹿县| 南靖县| 万山特区| 金门县| 武汉市| 广宗县| 通化市| 肇州县| 诏安县| 弥勒县| 夏河县| 江永县| 景泰县| 东乡| 洛川县| 广灵县| 汕尾市| 浠水县| 肇庆市| 莱阳市| 昭平县| 黑水县| 连江县| 无为县| 丰台区| 田东县| 三门县| 澄迈县| 天镇县| 淮阳县|