首页技术html动画效果代码,html5简单的动画

html动画效果代码,html5简单的动画

编程之家2026-05-30680次浏览

大家好,关于html动画效果代码很多朋友都还不太明白,今天小编就来为大家分享关于html5简单的动画的知识,希望对各位有所帮助!

html动画效果代码,html5简单的动画

如何制作html5的动画效果

主要思想:\x0d\x0a首先要准备一张有连续帧的图片,然后利用HTML5Canvas的draw方法在不同的时间间隔绘制不同的帧,这样看起来就像动画在播放。\x0d\x0a关键技术点:\x0d\x0aJavaScript函数setTimeout()有两个参数,第一个是参数可以传递一个JavaScript方法,\x0d\x0a另外一个参数代表间隔时间,单位为毫秒数。代码示例:\x0d\x0asetTimeout(update,1000/30);\x0d\x0aCanvas的API-drawImage()方法,需要指定全部9个参数:\x0d\x0actx.drawImage(myImage,offw,offh,width,height,x2,y2,width,height);\x0d\x0a其中offw,offh是指源图像的起始坐标点,width,height表示源图像的宽与高,x2,y2表\x0d\x0a示源图像在目标Canvas上的起始坐标点。\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aCanvasMouseEventDemo\x0d\x0a\x0d\x0a\x0d\x0avarctx=null;//globalvariable2dcontext\x0d\x0avarstarted=false;\x0d\x0avarmText_canvas=null;\x0d\x0avarx=0,y=0;\x0d\x0avarframe=0;//225*5+2\x0d\x0avarimageReady=false;\x0d\x0avarmyImage=null;\x0d\x0avarpx=300;\x0d\x0avarpy=300;\x0d\x0avarx2=300;\x0d\x0avary2=0;\x0d\x0awindow.onload=function(){\x0d\x0avarcanvas=document.getElementById("animation_canvas");\x0d\x0aconsole.log(canvas.parentNode.clientWidth);\x0d\x0acanvas.width=canvas.parentNode.clientWidth;\x0d\x0acanvas.height=canvas.parentNode.clientHeight;\x0d\x0aif(!canvas.getContext){\x0d\x0aconsole.log("Canvasnotsupported.PleaseinstallaHTML5compatiblebrowser.");\x0d\x0areturn;\x0d\x0a}\x0d\x0a//get2Dcontextofcanvasanddrawrectangel\x0d\x0actx=canvas.getContext("2d");\x0d\x0actx.fillStyle="black";\x0d\x0actx.fillRect(0,0,canvas.width,canvas.height);\x0d\x0amyImage=document.createElement('img');\x0d\x0amyImage.src="../robin.png";\x0d\x0amyImage.onload=loaded();\x0d\x0a}\x0d\x0afunctionloaded(){\x0d\x0aimageReady=true;\x0d\x0asetTimeout(update,1000/30);\x0d\x0a}\x0d\x0afunctionredraw(){\x0d\x0actx.clearRect(0,0,460,460)\x0d\x0actx.fillStyle="black";\x0d\x0actx.fillRect(0,0,460,460);\x0d\x0a//findtheindexofframesinimage\x0d\x0avarheight=myImage.naturalHeight/5;\x0d\x0avarwidth=myImage.naturalWidth/5;\x0d\x0avarrow=Math.floor(frame/5);\x0d\x0avarcol=frame-row*5;\x0d\x0avaroffw=col*width;\x0d\x0avaroffh=row*height;\x0d\x0a//firstrobin\x0d\x0apx=px-5;\x0d\x0apy=py-5;\x0d\x0aif(px=22)frame=0;\x0d\x0asetTimeout(update,1000/30);\x0d\x0a}\x0d\x0a\x0d\x0a\x0d\x0a\x0d\x0aHTMLCanvasAnimationsDemo-ByGloomyFish\x0d\x0aPlayAnimations\x0d\x0a\x0d\x0a\x0d\x0a

HTML5动画特效怎么做

主要思想:

首先要准备一张有连续帧的图片,然后利用HTML5 Canvas的draw方法在不同的时间间隔绘制不同的帧,这样看起来就像动画在播放。

关键技术点:

JavaScript函数setTimeout()有两个参数,第一个是参数可以传递一个JavaScript方法,

另外一个参数代表间隔时间,单位为毫秒数。代码示例:

html动画效果代码,html5简单的动画

setTimeout( update, 1000/30);

Canvas的API-drawImage()方法,需要指定全部9个参数:

ctx.drawImage(myImage, offw, offh, width,height, x2, y2, width, height);

其中offw, offh是指源图像的起始坐标点,width, height表示源图像的宽与高,x2,y2表

示源图像在目标Canvas上的起始坐标点。

<!DOCTYPEhtml>

html动画效果代码,html5简单的动画

<html>

<head>

<metahttp-equiv="X-UA-Compatible"content="chrome=IE8">

<metahttp-equiv="Content-type"content="text/html;charset=UTF-8">

<title>CanvasMouseEventDemo</title>

<linkrel="external nofollow" href="default.css"rel="stylesheet"/>

<script>

varctx=null;//globalvariable2dcontext

varstarted=false;

varmText_canvas=null;

varx=0,y=0;

varframe=0;//225*5+2

varimageReady=false;

varmyImage=null;

varpx=300;

varpy=300;

varx2=300;

vary2=0;

window.onload=function(){

varcanvas=document.getElementById("animation_canvas");

console.log(canvas.parentNode.clientWidth);

canvas.width=canvas.parentNode.clientWidth;

canvas.height=canvas.parentNode.clientHeight;

if(!canvas.getContext){

console.log("Canvasnotsupported.PleaseinstallaHTML5compatiblebrowser.");

return;

}

//get2Dcontextofcanvasanddrawrectangel

ctx=canvas.getContext("2d");

ctx.fillStyle="black";

ctx.fillRect(0,0,canvas.width,canvas.height);

myImage=document.createElement('img');

myImage.src="../robin.png";

myImage.onload=loaded();

}

functionloaded(){

imageReady=true;

setTimeout(update,1000/30);

}

functionredraw(){

ctx.clearRect(0,0,460,460)

ctx.fillStyle="black";

ctx.fillRect(0,0,460,460);

//findtheindexofframesinimage

varheight=myImage.naturalHeight/5;

varwidth=myImage.naturalWidth/5;

varrow=Math.floor(frame/5);

varcol=frame-row*5;

varoffw=col*width;

varoffh=row*height;

//firstrobin

px=px-5;

py=py-5;

if(px<-50){

px=300;

}

if(py<-50){

py=300;

}

//varrate=(frame+1)/22;

//varrw=Math.floor(rate*width);

//varrh=Math.floor(rate*height);

ctx.drawImage(myImage,offw,offh,width,height,px,py,width,height);

//secondrobin

x2=x2-5;

y2=y2+5;

if(x2<-50){

x2=300;

y2=0;

}

ctx.drawImage(myImage,offw,offh,width,height,x2,y2,width,height);

}

functionupdate(){

redraw();

frame++;

if(frame>=22)frame=0;

setTimeout(update,1000/30);

}

</script>

</head>

<body>

<h1>HTMLCanvasAnimationsDemo-ByGloomyFish</h1>

<pre>PlayAnimations</pre>

<divid="my_painter">

<canvasid="animation_canvas"></canvas>

</div>

</body>

</html>

css如何实现无限轮播图动画(代码示例)

本篇文章给大家带来的内容是如何实现自动无限播放的轮播图动画效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

在之前的文章【css如何实现图片的旋转展示效果】中介绍了手动无限轮播图的制作,本篇我们来看看自动无限轮播图动画的制作。下面我们就来看看动画效果是如何实现的。

1、设置动画的舞台

HTML与之前文章里的示例非常相似。我们要有一个动画发生的舞台(#stage),一个将会旋转的div容器和一系列图像:

<div id="stage">

<div id="rotator" style="-webkit-animation-name: rotator;">

<a rel="external nofollow" href="1.jpg"><img src="1.jpg" width="140"></a>

<a rel="external nofollow" href="2.jpg"><img src="2.jpg" width="140"></a>

<a rel="external nofollow" href="3.jpg"><img src="3.jpg" width="140"></a>

<a rel="external nofollow" href="4.jpg"><img src="4.jpg" width="140"></a>

<a rel="external nofollow" href="5.jpg"><img src="5.jpg" width="140"></a>

<a rel="external nofollow" href="6.jpg"><img src="6.jpg" width="140"></a>

<a rel="external nofollow" href="7.jpg"><img src="7.jpg" width="140"></a>

<a rel="external nofollow" href="8.jpg"><img src="8.jpg" width="140"></a>

</div>

</div>内联样式属性引用下面的动画@keyframes。它需要内联而不是CSS,以便我们能够使用JavaScript停止和重新启动动画。

2、在3D空间中布置照片

CSS样式用于定位多张照片,首先围绕y轴旋转它们(垂直向上翻页),然后径向向外平移:

#stage{

margin: 2em auto 1em 50%;

height: 140px;

-webkit-perspective: 1200px;

-webkit-perspective-origin: 0 50%;

}

#rotator a{

position: absolute;

left:-81px;

}

#rotator a img{

padding: 10px;

border: 1px solid#ccc;

background:#fff;

-webkit-backface-visibility: hidden;

}

#rotator a:nth-of-type(1) img{

-webkit-transform: rotateY(-90deg) translateZ(300px);

}

#rotator a:nth-of-type(2) img{

-webkit-transform: rotateY(-60deg) translateZ(300px);

}

#rotator a:nth-of-type(3) img{

-webkit-transform: rotateY(-30deg) translateZ(300px);

}

#rotator a:nth-of-type(4) img{

-webkit-transform: translateZ(300px);

background:#000;

}

#rotator a:nth-of-type(5) img{

-webkit-transform: rotateY(30deg) translateZ(300px);

}

#rotator a:nth-of-type(6) img{

-webkit-transform: rotateY(60deg) translateZ(300px);

}

#rotator a:nth-of-type(n+7){ display: none;}照片设置-81px的值代表向左移动,使前向照片在旋转轴上居中。距离是图像宽度的一半(140px/ 2)加上LHS图像填充(10px)和边框(1px)。

该阶段需要设置立体的动画,舞台从页面的中心开始,因此旋转元素下的锚元素需要向后移动以使动画居中。

我们只开始准备六张照片,左边三张,中间一张,右边两张。最左侧的照片(位置1)从左侧开始,因此仅在第一次旋转后才可见。

当照片旋转时,它会消失(显示:无),并且新照片会附加到左侧,准备从位置1旋转。在7和更高的位置可以有任意数量的照片。只有当它们移动到可见位置时,它们才会出现。

甚至可以在动画进行时使用Ajax加载新照片。

3、添加动画效果

正如我们之前尝试的那样,不是将照片轮旋转整整360度,而是我们实际做的只是旋转30度(足以从一张照片到下一张照片):

@-webkit-keyframes rotator{

from{-webkit-transform: rotateY(0deg);}

to{-webkit-transform: rotateY(30deg);}

}

#rotator{

-webkit-transform-origin: 0 0;

-webkit-transform-style: preserve-3d;

-webkit-animation-timing-function: cubic-bezier(1, 0.2, 0.2, 1);

-webkit-animation-duration: 1s;

-webkit-animation-delay: 1s;

}

#rotator:hover{

-webkit-animation-play-state: paused;

}要使关键帧在其他浏览器中工作,请复制所有样式,替换-webkit- with-moz-和-ms-,如下面的示例代码块所示。

动画完成后,它会触发一个JavaScript事件,您可以在下一节中阅读该事件。事件处理程序沿着照片移动,以便在动画重置时,而不是回到初始状态,照片都围绕圆圈移动了一步。

为了更清楚地了解正在发生的事情,中心照片已在下面的演示中突出显示。在动画发生时,您将看到突出显示的节点旋转,然后重置回起始位置,但包含不同的照片。

4、 JavaScript添加动画控制器

我们在此示例中需要JavaScript来检测动画何时结束,以便协调通过车轮重置移动的照片。没有这个,轮子只会在前两张照片之间交替出现。

document.addEventListener("DOMContentLoaded", function(){

var rotateComplete= function(){

target.style.webkitAnimationName="";

target.insertBefore(arr[arr.length-1], arr[0]);

setTimeout(function(el){

el.style.webkitAnimationName="rotator";

}, 0, target);

};

var target= document.getElementById("rotator");

var arr= target.getElementsByTagName("a");

target.addEventListener("webkitAnimationEnd", rotateComplete, false);

}, false);对于每个WebKit样式和其他引用,存在Firefox(-moz-或Moz),Opera(-o-或O),甚至Internet Explorer(-ms-或ms)的替代品-我们必须忍受的混乱直到标准最终确定。

要在Safari,Chrome,Firefox,Opera和Internet Explorer 10中使用此功能,我们需要包含以下额外的设置:

var rotateComplete= function(){

with(target.style){

webkitAnimationName= MozAnimationName= msAnimationName="";

}

target.insertBefore(arr[arr.length-1], arr[0]);

setTimeout(function(el){

with(el.style){

webkitAnimationName= MozAnimationName= msAnimationName="rotator";

}

}, 0, target);

};

var target= document.getElementById("rotator");

var arr= target.getElementsByTagName("a");

target.addEventListener("webkitAnimationEnd", rotateComplete, false);

target.addEventListener("animationend", rotateComplete, false);

target.addEventListener("MSAnimationEnd", rotateComplete, false);目前尚不清楚为何需要setTimeout。我们不需要它来设置延迟,因为使用CSS完成,但没有setTimeout(甚至0ms)动画无法重新触发。

5、效果展示

这只是横向的轮播,在之后的文章【css实现三面立体旋转无限轮播图动画】里会在本篇文章的基础上改进轮播方式,制作不一样的的轮播图动画。

文章分享结束,html动画效果代码和html5简单的动画的答案你都知道了吗?欢迎再次光临本站哦!

小说网站php源码 php源码网小学生有必要学编程吗,小学生学编程到底有没有用