原生javascript實(shí)現(xiàn)的一個(gè)簡單動(dòng)畫效果
來源:易賢網(wǎng) 閱讀:998 次 日期:2016-07-14 16:21:48
溫馨提示:易賢網(wǎng)小編為您整理了“原生javascript實(shí)現(xiàn)的一個(gè)簡單動(dòng)畫效果”,方便廣大網(wǎng)友查閱!

本文章向大家介紹一個(gè)javascript實(shí)現(xiàn)的動(dòng)畫。點(diǎn)擊開始按鈕div會往右移動(dòng),點(diǎn)擊停止后,div停止移動(dòng),再點(diǎn)擊則繼續(xù)移動(dòng)。請看下面代碼。

<!DOCTYPE html> 

<html> 

<head> 

<meta charset=" utf-8"> 

<head>

<title>javascript實(shí)現(xiàn)的簡單動(dòng)畫</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="開始運(yùn)動(dòng)" />

<input type="button" id="stopmove" value="停止運(yùn)動(dòng)" />

<div id="mydiv"></div>

</body>

</html>

代碼解釋:

1.window.onload=function(){},當(dāng)文檔內(nèi)容完全加載完畢再去執(zhí)行函數(shù)中的代碼。

2.var mydiv=document.getElementById("mydiv"),獲取id屬性值為mydiv的元素。

3.var start=document.getElementById("start"),獲取id屬性hi為start的元素。

4.var stopmove=document.getElementById("stopmove"),獲取id屬性值為stopmove的元素。

5.mydiv.style.left=x+"px",設(shè)置div的left屬性值。

6.start.onclick=function(){},為start元素注冊onclick事件處理函數(shù)。

7.clearInterval(flag),停止定時(shí)器函數(shù),一方多次單擊開始按鈕造成疊加效果。

8.flag=setInterval(move,20),開始運(yùn)動(dòng)。

以上這篇原生javascript實(shí)現(xiàn)的一個(gè)簡單動(dòng)畫效果就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考

更多信息請查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)