国产探花免费观看_亚洲丰满少妇自慰呻吟_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>

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秀山| 安阳市| 光泽县| 临漳县| 蚌埠市| 丹巴县| 奇台县| 旅游| 历史| 新兴县| 浦县| 曲麻莱县| 江达县| 东山县| 华亭县| 巢湖市| 台南县| 波密县| 德兴市| 池州市| 虎林市| 定南县| 贵州省| 安国市| 山西省| 安化县| 神农架林区| 错那县| 新余市| 沂源县| 屯门区| 泌阳县| 军事| 固始县| 洪洞县| 钟山县| 南平市| 射阳县| 锦州市| 石家庄市| 新绛县|