你可以使用JavaScript來(lái)創(chuàng)建復(fù)雜的動(dòng)畫(huà)其中包括但不限于:
本教程會(huì)給一個(gè)基本的了解如何使用JavaScript來(lái)創(chuàng)建一個(gè)動(dòng)畫(huà)。
JavaScript可以按照某種模式,由一個(gè)邏輯等式或函數(shù)來(lái)確定移動(dòng)至若干DOM元素(<IMG/>,<DIV>或任何其他HTML元素)頁(yè)面各處。
JavaScript提供以下要經(jīng)常用于動(dòng)畫(huà)程序兩種功能。
setTimeout( function, duration) - 從現(xiàn)在這個(gè)函數(shù)調(diào)用 duration 毫秒后的 function
setInterval(function, duration) - 每次持續(xù)duration 毫秒之后,此函數(shù)調(diào)用function。
clearTimeout(setTimeout_variable) - 這個(gè)函數(shù)調(diào)用清除任何計(jì)時(shí)器由setTimeout()函數(shù)設(shè)置。
JavaScript還可以設(shè)置一個(gè)數(shù)字,包括它在屏幕上的位置DOM對(duì)象的屬性??梢栽O(shè)置一個(gè)對(duì)象的頂部和左側(cè)的屬性在屏幕上的任何位置。下面是簡(jiǎn)單的語(yǔ)法:
// Set distance from left edge of the screen. object.style.left = distance in pixels or points; or // Set distance from top edge of the screen. object.style.top = distance in pixels or points;
所以,讓我們使用DOM對(duì)象的屬性和JavaScript函數(shù)如下的實(shí)現(xiàn)一個(gè)簡(jiǎn)單的動(dòng)畫(huà):
<html>
<head>
<title>JavaScript Animation</title>
<script type="text/javascript">
<!--
var imgObj = null;
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
}
function moveRight(){
imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
}
window.onload =init;
//-->
</script>
</head>
<body>
<form>
<img id="myImage" src="/images/html.gif" />
<p>Click button below to move the image to right</p>
<input type="button" value="Click Me" onclick="moveRight();" />
</form>
</body>
</html>
下面是上面的例子的說(shuō)明:
我們使用的是JavaScript函數(shù)的getElementById()來(lái)獲取一個(gè)DOM對(duì)象,然后將其分配給一個(gè)全局變量 imgObj.
我們定義了一個(gè)初始化函數(shù)的init()來(lái)初始化imgObj,我們已經(jīng)設(shè)置它的位置和左屬性。
我們調(diào)用初始化函數(shù)在窗口加載時(shí)。
最后,我們調(diào)用并將MoveRight()函數(shù)由10個(gè)像素來(lái)增加左邊的距離。你也可以將其設(shè)置為一個(gè)負(fù)數(shù)值,以將其移動(dòng)到左側(cè)。
在上面的例子中,如我們所看到的,如何將圖像移動(dòng)到右每點(diǎn)擊??梢酝ㄟ^(guò)使用JavaScript函數(shù)的setTimeout()如下自動(dòng)完成這一過(guò)程:
<html>
<head>
<title>JavaScript Animation</title>
<script type="text/javascript">
<!--
var imgObj = null;
var animate ;
function init(){
imgObj = document.getElementById('myImage');
imgObj.style.position= 'relative';
imgObj.style.left = '0px';
}
function moveRight(){
imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
animate = setTimeout(moveRight,20); // call moveRight in 20msec
}
function stop(){
clearTimeout(animate);
imgObj.style.left = '0px';
}
window.onload =init;
//-->
</script>
</head>
<body>
<form>
<img id="myImage" src="/images/html.gif" />
<p>Click the buttons below to handle animation</p>
<input type="button" value="Start" onclick="moveRight();" />
<input type="button" value="Stop" onclick="stop();" />
</form>
</body>
</html>
在這里,我們?cè)黾痈嗟那槿ぁR虼?,讓我們看看新的功能?/p>
并將 MoveRight()函數(shù)調(diào)用 setTimeout()函數(shù)來(lái)設(shè)置 imgObj 位置。
我們?cè)黾恿艘粋€(gè)新的函數(shù)stop()來(lái)清除由定時(shí)器設(shè)定的setTimeout()函數(shù)來(lái)設(shè)置對(duì)象在其初始位置。
下面是一個(gè)簡(jiǎn)單的例子,顯示圖像翻轉(zhuǎn)用鼠標(biāo)事件:
<html>
<head>
<title>Rollover with a Mouse Events</title>
<script type="text/javascript">
<!--
if(document.images){
var image1 = new Image(); // Preload an image
image1.src = "/images/html.gif";
var image2 = new Image(); // Preload second image
image2.src = "/images/http.gif";
}
//-->
</script>
</head>
<body>
<p>Move your mouse over the image to see the result</p>
<a href="#" onMouseOver="document.myImage.src=image2.src;"
onMouseOut="document.myImage.src=image1.src;">
<img name="myImage" src="/images/html.gif" />
</a>
</body>
</html>
讓我們來(lái)看看有什么不同的位置:
在加載這個(gè)頁(yè)面,if語(yǔ)句檢查圖像對(duì)象存在的時(shí)間。如果圖像對(duì)象是不可用的,該塊將不被執(zhí)行
Image()構(gòu)造函數(shù)創(chuàng)建并預(yù)裝名為image1的一個(gè)新的圖像對(duì)象
src屬性指定的外部圖像文件的名稱叫 /images/html.gif
我們已經(jīng)創(chuàng)建IMAGE2對(duì)象,并在這個(gè)對(duì)象分配/images/http.gif類似的方式
在#(井號(hào))禁用鏈接,瀏覽器不會(huì)嘗試去一個(gè)URL點(diǎn)擊時(shí)。此鏈接的圖像
當(dāng)用戶的鼠標(biāo)移動(dòng)到鏈路,而onmouseout事件處理程序,當(dāng)用戶的鼠標(biāo)移動(dòng)遠(yuǎn)離鏈路(圖像)被觸發(fā)onMouseOver事件處理程序被觸發(fā)
當(dāng)鼠標(biāo)移動(dòng)時(shí)在圖像上,從第一圖像到第二個(gè)在HTTP圖像的變化。當(dāng)鼠標(biāo)被從圖像移離,則顯示原來(lái)的圖象
當(dāng)鼠標(biāo)離開(kāi)該鏈接時(shí),初始圖像html.gif將重新出現(xiàn)在屏幕上