今日所學1

  今日也主要學習js,但更爲深刻。html

  首先是全局變量及局部變量。全局變量能夠在整個程序內進行引用,而局部變量只能在其定義的函數內使用。在平時編程過程當中,須要特別注意什麼時候該用何種變量。巧用變量會獲得出乎意料的結果。同事在js中鏈接字符串、數字及變量採用'+'。最後學習了定時器及延時器的用法,定時器是每隔一段時間進行調用某個函數,一直循環,直到清除定時器,而延時器是隔一段時間才調用某個函數。通過今天的學習,以及老師的指點,自行編程,完成了跑馬燈效果製做。其代碼以下:編程

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<title>無標題文檔</title>
</head>dom

<body>
<input type="button" value="跑馬燈" id="but1" onClick="change1()"/>
<input type="button" value="中止" id="but2" / onClick="stop1()">
<div style="height:100px; width:100px; background:rgba(0,255,0,1); margin-left:0px; margin-top:0px;" id="div1"></div>
</body>
</html>
<script>
var timer;
var fx=0;
var obtd=document.getElementById('div1');
function change1(){
timer=setInterval(change,100);
function change(){
if(parseInt(obtd.style.marginLeft)==500&&parseInt(obtd.style.marginTop)!=500){fx=1;}
else if(parseInt(obtd.style.marginTop)==500&&parseInt(obtd.style.marginLeft)==500){fx=2;}
else if(parseInt(obtd.style.marginLeft)==0&&parseInt(obtd.style.marginTop)==500){fx=3;}
else if(parseInt(obtd.style.marginTop)==0&&parseInt(obtd.style.marginLeft)==0){fx=0;}
if(fx==0){moveright();}
else if(fx==1){movebottom();}
else if(fx==2){moveleft();}
else if(fx==3){movetop();}
}
}
function stop1(){
clear=clearInterval(timer);
}
function moveright(){
var me3="#"+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16);
obtd.style.backgroundColor=me3;
obtd.style.marginLeft=(parseInt(obtd.style.marginLeft)+10)+'px';
//console.log(obtd.style.marginLeft);
}

function moveleft(){
var me3="#"+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16);
obtd.style.backgroundColor=me3;
obtd.style.marginLeft=(parseInt(obtd.style.marginLeft)-10)+'px';
//console.log(obtd.style.marginLeft);
}
function movebottom(){
var me3="#"+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16);
obtd.style.backgroundColor=me3;
obtd.style.marginTop=(parseInt(obtd.style.marginTop)+10)+'px';
//console.log(obtd.style.marginLeft);
}
function movetop(){
var me3="#"+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16)+Math.floor(Math.random()*256).toString(16);
obtd.style.backgroundColor=me3;
obtd.style.marginTop=(parseInt(obtd.style.marginTop)-10)+'px';
//console.log(obtd.style.marginLeft);
}
</script>函數

相關文章
相關標籤/搜索