範例使用HTML5的canvas標籤和Javascript腳本,簡單的編寫了裝載圖片效果,請使用支援HTML5的瀏覽器預覽效果:
下圖為以逐漸橫向柵格的效果圖
html部分:
XML/HTML Code複製內容到剪貼簿
- html>
-
html lang=lang=lang 🎜>
>-
head
>-
meta charset="UTF-8 "
>-
title>html5 裝載圖片html5 裝載圖片
title-
>
head-
>
body-
>
button onclick=onclick=onclick= "
- >由左至右button>button> 🎜>
button onclick=
- onclick=onclick==>從中央到左右兩邊button>button>button
- button onclick=onclick
- =onclick= ">以逐漸橫向柵格button>button>button
hr/>
canvas class=class=
- class=class=class=class=
- class=class= id=
"canvas"
"600"
height="300"
> /canvas>
body>
html>
JavaScript部分:
XML/HTML Code複製內容到剪貼簿
- //初始化
-
var canvas = document
-
context = canvas.getContext("2d"),
image-
= new Image();
image.src
= -
"img/test.jpg";
//由左至右邊負荷法
function drawImg1(){
- var
drawWidth- =
0-
, , , ,
interval = setInterval(function(){ (function(){
context.drawImage(image, 0, 0, drawWidth, image.height, 0, 0, drawWidth, image.height); > -
drawWidth = 20; -
if(drawWidth - >
canvas.width) clearInterval(interval); -
- },100);
}
//從中央向左右兩邊拉開加載方法
- function drawImg2(){
var drawWidth = 0
- , , ,
drawLeft- =
canvas- .width/2,
-
interval = setInterval(function(){ (function(){
context.drawImage(image, drawLeft, 0, drawWidth, image.height, drawLeft, 0, drawWidth, image.height);
drawWidth = 20;
- drawLeft
-- =
10-
; 10
- ;
- if(drawLeft
0) clearInterval(interval);
},100); -
}
//以逐漸橫向柵格負載法
function drawImg3(){
-
var drawWidth = 0,
-
, , ,
spaceWidth = canvas- .width/20, //10
- interval
= - setInterval
(function(){ -
(function(){
for(var i = 0;i0;i0;i0;i0;i20;i ){
context.drawImage(image, i*spaceWidth, 0, drawWidth, image.height, i*spaceWidth, 0, drawWidth, image.height);
}
drawWidth = 5;
if(drawWidth > spaceWidth) clearInterval(interval);
},100);
- }
以上內容是小編給大家介紹的HTML5 用動畫的表現形式裝載圖像,希望對大家有幫助!