原生javascript實(shí)現(xiàn)勻速運(yùn)動(dòng)動(dòng)畫(huà)效果
本文向大家介紹一個(gè)javascript實(shí)現(xiàn)的動(dòng)畫(huà)。點(diǎn)擊開(kāi)始按鈕div會(huì)往右移動(dòng),點(diǎn)擊停止后,div停止移動(dòng),再點(diǎn)擊則繼續(xù)移動(dòng)。請(qǐng)看下面代碼:
<html>
<head>
<meta charset="gb2312">
<head>
<title>javascript實(shí)現(xiàn)的簡(jiǎn)單動(dòng)畫(huà)</title>
<style type="text/css">
#mydiv
{
width:50px;
height:50px;
background-color:green;
position:absolute;
}
</style>
<script type="text/javascript">
window.onload=function()
{
var mydiv=document.getElementById("mydiv");
var start=document.getElementById("start");
var stopmove=document.getElementById("stopmove");
var x=0;
var flag;
function move()
{
x=x+1;
mydiv.style.left=x+"px";
}
start.onclick=function()
{
clearInterval(flag);
flag=setInterval(move,20);
}
stopmove.onclick=function()
{
clearInterval(flag);
}
}
</script>
<body>
<input type="button" id="start" value="開(kāi)始運(yùn)動(dòng)" />
<input type="button" id="stopmove" value="停止運(yùn)動(dòng)" />
<div id="mydiv"></div>
</body>
</html>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
javascript原始值和對(duì)象引用實(shí)例分析
這篇文章主要介紹了javascript原始值和對(duì)象引用的方法,實(shí)例分析了javascript原始值和對(duì)象引用的功能、定義與相關(guān)技巧,需要的朋友可以參考下2015-04-04
JScript內(nèi)置對(duì)象Array中元素的刪除方法
JScript內(nèi)置對(duì)象Array中元素的刪除方法...2007-03-03
js實(shí)現(xiàn)圖片放大并跟隨鼠標(biāo)移動(dòng)特效
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片放大并跟隨鼠標(biāo)移動(dòng)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
js window.onload 加載多個(gè)函數(shù)的方法
平時(shí)做項(xiàng)目 經(jīng)常需要使用window.onload,但window.onload 不能同時(shí)加載多個(gè)函數(shù)。2009-11-11
javaScript 動(dòng)態(tài)訪問(wèn)JSon元素示例代碼
訪問(wèn)JSon元素的方法有很多,在搜的時(shí)候會(huì)找到很多,本文使用javascript來(lái)動(dòng)態(tài)訪問(wèn)json元素,感興趣的朋友可以練練手哦2013-08-08

