你可以使用JavaScript來創(chuàng)建復(fù)雜的動畫其中包括但不限于:
本教程會給一個基本的了解如何使用JavaScript來創(chuàng)建一個動畫。
JavaScript可以按照某種模式,由一個邏輯等式或函數(shù)來確定移動至若干DOM元素(<IMG/>,<DIV>或任何其他HTML元素)頁面各處。
JavaScript提供以下要經(jīng)常用于動畫程序兩種功能。
setTimeout( function, duration) - 從現(xiàn)在這個函數(shù)調(diào)用 duration 毫秒后的 function
setInterval(function, duration) - 每次持續(xù)duration 毫秒之后,此函數(shù)調(diào)用function。
clearTimeout(setTimeout_variable) - 這個函數(shù)調(diào)用清除任何計時器由setTimeout()函數(shù)設(shè)置。
JavaScript還可以設(shè)置一個數(shù)字,包括它在屏幕上的位置DOM對象的屬性。可以設(shè)置一個對象的頂部和左側(cè)的屬性在屏幕上的任何位置。下面是簡單的語法:
// 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對象的屬性和JavaScript函數(shù)如下的實現(xiàn)一個簡單的動畫:
<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>
下面是上面的例子的說明:
我們使用的是JavaScript函數(shù)的getElementById()來獲取一個DOM對象,然后將其分配給一個全局變量 imgObj.
我們定義了一個初始化函數(shù)的init()來初始化imgObj,我們已經(jīng)設(shè)置它的位置和左屬性。
我們調(diào)用初始化函數(shù)在窗口加載時。
最后,我們調(diào)用并將MoveRight()函數(shù)由10個像素來增加左邊的距離。你也可以將其設(shè)置為一個負數(shù)值,以將其移動到左側(cè)。
在上面的例子中,如我們所看到的,如何將圖像移動到右每點擊。可以通過使用JavaScript函數(shù)的setTimeout()如下自動完成這一過程:
<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>
在這里,我們增加更多的情趣。因此,讓我們看看新的功能:
并將 MoveRight()函數(shù)調(diào)用 setTimeout()函數(shù)來設(shè)置 imgObj 位置。
我們增加了一個新的函數(shù)stop()來清除由定時器設(shè)定的setTimeout()函數(shù)來設(shè)置對象在其初始位置。
下面是一個簡單的例子,顯示圖像翻轉(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>
讓我們來看看有什么不同的位置:
在加載這個頁面,if語句檢查圖像對象存在的時間。如果圖像對象是不可用的,該塊將不被執(zhí)行
Image()構(gòu)造函數(shù)創(chuàng)建并預(yù)裝名為image1的一個新的圖像對象
src屬性指定的外部圖像文件的名稱叫 /images/html.gif
我們已經(jīng)創(chuàng)建IMAGE2對象,并在這個對象分配/images/http.gif類似的方式
在#(井號)禁用鏈接,瀏覽器不會嘗試去一個URL點擊時。此鏈接的圖像
當(dāng)用戶的鼠標(biāo)移動到鏈路,而onmouseout事件處理程序,當(dāng)用戶的鼠標(biāo)移動遠離鏈路(圖像)被觸發(fā)onMouseOver事件處理程序被觸發(fā)
當(dāng)鼠標(biāo)移動時在圖像上,從第一圖像到第二個在HTTP圖像的變化。當(dāng)鼠標(biāo)被從圖像移離,則顯示原來的圖象
當(dāng)鼠標(biāo)離開該鏈接時,初始圖像html.gif將重新出現(xiàn)在屏幕上