当前位置: 首页 > news >正文

做网站联系电话百度百度百度一下

做网站联系电话,百度百度百度一下,宁波做网站,wordpress posts page用HTML5实现动画 要在HTML5中实现动画&#xff0c;可以使用以下几种方法&#xff1a;CSS动画、使用<canvas>元素和JavaScript来实现动画、使用JavaScript动画库。重点介绍前两种。 一、CSS动画 CSS3 动画&#xff1a;使用CSS3的动画属性和关键帧&#xff08;keyframes&…

用HTML5实现动画

要在HTML5中实现动画,可以使用以下几种方法:CSS动画、使用<canvas>元素和JavaScript来实现动画、使用JavaScript动画库。重点介绍前两种。

一、CSS动画

CSS3 动画:使用CSS3的动画属性和关键帧(keyframes)来创建动画效果。通过定义动画的开始状态、结束状态和过渡效果,可以实现平滑的动画效果。

先看一个简单的例子:

<html><head><meta charset="UTF-8" /><title>在HTML5中用CSS3实现简单动画</title><style>.box {width: 100px;height: 100px;background-color: red;animation: myAnimation 2s infinite;}@keyframes myAnimation {0% { transform: translateX(0px); }50% { transform: translateX(200px); }100% { transform: translateX(0px); }}</style></head><body>  <div class="box"></div>  </body>
</html>

我这里命名为:CSS3简单动画.html

用浏览器打开,运行效果:

下面给出一个小车动画

由两部分组成:

HTML文件和CSS文件,为方便使用,我将这两个文件放在同一文件夹中。

HTML文件,我这里命名为:CSS3小车动画.html,源码如下:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>在HTML5中用CSS3实现动画</title><link rel="stylesheet" type="text/css" href="car.css"></head><body><div id="container"><div id="car"><div id="chassis"></div><div id="backtire" class="tire"><div class="hr"></div><div class="vr"></div></div><div id="fronttire" class="tire"><div class="hr"></div><div class="vr"></div></div>	</div><div id="grass"></div></div></body>
</html>

CSS文件,我这里命名为:car.css,源码如下:

 /*定义动画:从-400px的位置移动到1600px的位置 */@keyframes carAnimation {0% { left: -400px; }  /* 指定初始位置*/100% { left: 1600px; }  /* 指定最终位置*/}#car {position: absolute;width: 400px;height: 210px;top: 300px;left: 50px;animation: carAnimation 10s infinite linear;}#chassis {position: absolute;width: 400px;height: 130px;background: #FF9900;border: 2px solid #FF6600;}.tire {position: absolute;bottom: 0;height: 120px;width: 120px;border-radius: 60px;background: #0099FF;border: 1px solid #3300FF;animation: tyreAnimation 10s infinite linear;}#fronttire {right: 20px;}#backtire {left: 20px;}@keyframes tyreAnimation {0% { transform: rotate(0); }100% { transform: rotate(1800deg); }}#grass {position: absolute;width: 100%;height: 130px;bottom: 0;background: linear-gradient(bottom, #33CC00, #66FF22);}.hr {position: absolute;background: #3300FF;height: 2px;width: 100%;top: 60px;}.vr {position: absolute;background: #3300FF;width: 2px;height: 100%;left: 60px;top: 0;}

我这里命名为:CSS3简单动画.html

用浏览器打开,运行效果:

二、使用<canvas>元素和JavaScript来实现动画

先看一个简单的例子:

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8" /><title>在HTML5中用canvas+JS简单动画</title>  
</head>
<body>
<canvas id="myCanvas" width="400" height="200"></canvas>
<script>var canvas = document.getElementById("myCanvas");var ctx = canvas.getContext("2d");var x = 0;var dx = 2; // 方块的移动速度以及方向function draw() {ctx.clearRect(0, 0, canvas.width, canvas.height);ctx.fillRect(x, 50, 50, 50);// 更新方块的位置x += dx;// 如果方块触碰到画布的右边缘或左边缘,反转方向if (x + 50 > canvas.width || x < 0) {dx = -dx;}requestAnimationFrame(draw);}draw();
</script>
</body>
</html>

我这里命名为:canvas+JS简单动画.html

运行效果:

下面给出一个小车动画

由两部分组成

HTML文件和JavaScript文件,为方便使用,我将这两个文件放在同一文件夹中。

HTML文件,我这里命名为:canvas+JS小车动画.html,源码如下:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>在HTML5中用canvas+JS小车动画</title><style>body {margin: 0;overflow: hidden;}canvas {display: block;}
</style>
</head>
<body><canvas id="canvas"></canvas><script src="car.js"></script>
</body>
</html>

JavaScript文件,我这里命名为:car.js,源码如下:

    const canvas = document.getElementById('canvas');const ctx = canvas.getContext('2d');// Set canvas full screencanvas.width = window.innerWidth;canvas.height = window.innerHeight-120;  //const car = {x: 50,y: canvas.height - 180,  //width: 200,height: 100,wheelRadius: 40,wheelOffset: 25,wheelRotation: 0};function drawCar(x, y, width, height, wheelRadius, wheelOffset, wheelRotation) {// Draw car bodyctx.fillStyle = 'orange';ctx.fillRect(x, y, width, height);// Draw wheelsconst wheelPositions = [{ x: x + wheelOffset, y: y + height },{ x: x + width - wheelOffset, y: y + height }];wheelPositions.forEach(wheelPos => {ctx.save();ctx.translate(wheelPos.x, wheelPos.y);ctx.rotate(wheelRotation);// Draw wheelctx.beginPath();ctx.arc(0, 0, wheelRadius, 0, Math.PI * 2);ctx.fillStyle = 'blue';ctx.fill();// Draw spokesctx.beginPath();ctx.moveTo(-wheelRadius, 0);ctx.lineTo(wheelRadius, 0);ctx.moveTo(0, -wheelRadius);ctx.lineTo(0, wheelRadius);ctx.strokeStyle = 'white';ctx.lineWidth = 4;ctx.stroke();ctx.restore();});}function animate() {ctx.clearRect(0, 0, canvas.width, canvas.height);// Draw groundctx.fillStyle = 'green';ctx.fillRect(0, canvas.height - 50, canvas.width, 50);// Update wheel rotationcar.wheelRotation += 0.05;// Draw cardrawCar(car.x, car.y, car.width, car.height, car.wheelRadius, car.wheelOffset, car.wheelRotation);// Move carcar.x += 2;if (car.x > canvas.width) {car.x = -car.width;}requestAnimationFrame(animate);}animate();

用浏览器打开,效果如下:

修改上面源码,将两个文件合二为一,并添加几个控制按钮“暂停/继续”、“快”、“慢”,并实现相关功能:

点击pauseResumeBtn按钮会切换动画的暂停和继续状态。

点击speedUpBtn按钮会增加小车的速度。

点击speedDownBtn按钮会减慢小车的速度,但速度不能小于1。

源码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>在HTML5中用canvas+JS小车可控动画</title>
<style>body {margin: 0;overflow: hidden;}canvas {display: block;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<button id="pauseResumeBtn">暂停/继续</button>
<button id="speedUpBtn">快</button>
<button id="speedDownBtn">慢</button>
<script>const canvas = document.getElementById('canvas');const ctx = canvas.getContext('2d');// Set canvas full screencanvas.width = window.innerWidth;canvas.height = window.innerHeight - 120;  //const car = {x: 50,y: canvas.height - 180,  //width: 200,height: 100,wheelRadius: 40,wheelOffset: 25,wheelRotation: 0,speed: 2};let isPaused = false;function drawCar(x, y, width, height, wheelRadius, wheelOffset, wheelRotation) {// Draw car bodyctx.fillStyle = 'orange';ctx.fillRect(x, y, width, height);// Draw wheelsconst wheelPositions = [{ x: x + wheelOffset, y: y + height },{ x: x + width - wheelOffset, y: y + height }];wheelPositions.forEach(wheelPos => {ctx.save();ctx.translate(wheelPos.x, wheelPos.y);ctx.rotate(wheelRotation);// Draw wheelctx.beginPath();ctx.arc(0, 0, wheelRadius, 0, Math.PI * 2);ctx.fillStyle = 'blue';ctx.fill();// Draw spokesctx.beginPath();ctx.moveTo(-wheelRadius, 0);ctx.lineTo(wheelRadius, 0);ctx.moveTo(0, -wheelRadius);ctx.lineTo(0, wheelRadius);ctx.strokeStyle = 'white';ctx.lineWidth = 4;ctx.stroke();ctx.restore();});}function animate() {ctx.clearRect(0, 0, canvas.width, canvas.height);// Draw groundctx.fillStyle = 'green';ctx.fillRect(0, canvas.height - 50, canvas.width, 50);// Update wheel rotationcar.wheelRotation += 0.05;// Draw cardrawCar(car.x, car.y, car.width, car.height, car.wheelRadius, car.wheelOffset, car.wheelRotation);// Move carcar.x += car.speed;if (car.x > canvas.width) {car.x = -car.width;}if (!isPaused) {requestAnimationFrame(animate);}}// Button event listenersdocument.getElementById('pauseResumeBtn').addEventListener('click', function() {isPaused = !isPaused;if (!isPaused) {animate();}});document.getElementById('speedUpBtn').addEventListener('click', function() {car.speed += 1;});document.getElementById('speedDownBtn').addEventListener('click', function() {if (car.speed > 1) {car.speed -= 1;}});animate();
</script>
</body>
</html>

我这里保存命名为:canvas+JS小车可控动画.html

用浏览器打开,效果如下:

三、使用JavaScript动画库

使用JavaScript动画库(如Anime.js、Velocity.js、Three.js等)可以更方便地创建复杂的动画效果,我没有深入学习了解,在此就不介绍了。

附录:

CSS3动画详解(图文教程) https://www.cnblogs.com/qianguyihao/p/8435182.html

anime.js 简单入门教程https://www.cnblogs.com/joyco773/p/10734850.html

Velocity.js 中文文档https://www.cnblogs.com/guandekuan/p/6643988.html


文章转载自:
http://wanjiahorah.xzLp.cn
http://wanjiahematoma.xzLp.cn
http://wanjiairreflexive.xzLp.cn
http://wanjiamurrelet.xzLp.cn
http://wanjiaresummon.xzLp.cn
http://wanjiailmenite.xzLp.cn
http://wanjiataxless.xzLp.cn
http://wanjiakickback.xzLp.cn
http://wanjiaventil.xzLp.cn
http://wanjiatungting.xzLp.cn
http://wanjiagrandee.xzLp.cn
http://wanjiaencephalomalacia.xzLp.cn
http://wanjiabashlyk.xzLp.cn
http://wanjiapyrolysate.xzLp.cn
http://wanjiaforgetter.xzLp.cn
http://wanjiaerubescent.xzLp.cn
http://wanjiawitted.xzLp.cn
http://wanjiaabdicate.xzLp.cn
http://wanjiahulloa.xzLp.cn
http://wanjiaredoubtable.xzLp.cn
http://wanjiaimmedicable.xzLp.cn
http://wanjiadulse.xzLp.cn
http://wanjiamonosemantic.xzLp.cn
http://wanjiascilicet.xzLp.cn
http://wanjiaseptemia.xzLp.cn
http://wanjiamatrix.xzLp.cn
http://wanjiabraise.xzLp.cn
http://wanjiabemoist.xzLp.cn
http://wanjiauaw.xzLp.cn
http://wanjiacrinkleroot.xzLp.cn
http://wanjialumber.xzLp.cn
http://wanjiadiphthongia.xzLp.cn
http://wanjiawbn.xzLp.cn
http://wanjiasublabial.xzLp.cn
http://wanjialavish.xzLp.cn
http://wanjiasartor.xzLp.cn
http://wanjiacircumvent.xzLp.cn
http://wanjiabismuth.xzLp.cn
http://wanjiateethridge.xzLp.cn
http://wanjiaminaret.xzLp.cn
http://wanjialindesnes.xzLp.cn
http://wanjiaahead.xzLp.cn
http://wanjiaprocuress.xzLp.cn
http://wanjiacapitatim.xzLp.cn
http://wanjiaabate.xzLp.cn
http://wanjiamillesimal.xzLp.cn
http://wanjialilt.xzLp.cn
http://wanjiamnemotechny.xzLp.cn
http://wanjiahemicellulose.xzLp.cn
http://wanjiadifferentiable.xzLp.cn
http://wanjiaappropriator.xzLp.cn
http://wanjiacalfdozer.xzLp.cn
http://wanjiascullery.xzLp.cn
http://wanjiadreamer.xzLp.cn
http://wanjiafourpence.xzLp.cn
http://wanjiahandbarrow.xzLp.cn
http://wanjiapurchase.xzLp.cn
http://wanjiaperivascular.xzLp.cn
http://wanjiarepublic.xzLp.cn
http://wanjiasightworthy.xzLp.cn
http://wanjiaastrobleme.xzLp.cn
http://wanjiasecularity.xzLp.cn
http://wanjiaindigotic.xzLp.cn
http://wanjiarda.xzLp.cn
http://wanjiadrawknife.xzLp.cn
http://wanjiafisherboat.xzLp.cn
http://wanjiasometimes.xzLp.cn
http://wanjiacannibalize.xzLp.cn
http://wanjiavibram.xzLp.cn
http://wanjiazakat.xzLp.cn
http://wanjianuff.xzLp.cn
http://wanjiacountervail.xzLp.cn
http://wanjiaforgivable.xzLp.cn
http://wanjiaimposturing.xzLp.cn
http://wanjiatrendiness.xzLp.cn
http://wanjiaumangite.xzLp.cn
http://wanjiavalentinite.xzLp.cn
http://wanjiafamiliarly.xzLp.cn
http://wanjiacrooked.xzLp.cn
http://wanjiafreewill.xzLp.cn
http://www.15wanjia.com/news/128488.html

相关文章:

  • 卖渔具的亲戚做网站网络营销企业有哪些
  • 网站 备案 中国 名字网络营销以什么为中心
  • 公司网站需要服务器吗windows优化大师官方免费下载
  • 海南网站建设推荐网站排名监控工具
  • 网站程序开发教程互联网搜索引擎有哪些
  • 怎么做阿里妈妈推广网站广告外链购买平台
  • nas里安装wordpressseo服务公司
  • 国外h5网站模板下载南通关键词优化平台
  • 美女做暧暧视频网站软件开发公司排名
  • 网站banner图尺寸站长工具亚洲
  • 深圳网站制作公司新闻湖南长沙疫情最新消息
  • wordpress建站环境搭建电商软文范例100字
  • 网站建设怎么招聘搜索引擎营销的特点是
  • 自媒体平台前十名上海seo公司
  • 网站建设 合优网络网络推广怎么样
  • 用易语言做刷网站注册软件网站如何优化关键词排名
  • wordpress cookie失效青岛seo建站
  • 做外贸翻译用那个网站十大软件培训机构
  • wordpress怎么注册用户百度推广账户优化
  • 锦州网站建设工作互联网推广运营
  • 做电商需要知道的几个网站百度公司简介介绍
  • 北京建设工程造价信息杭州网站优化体验
  • 网站description是指什么百度广告搜索推广
  • 那个网站专做代购香水的怎么优化网站性能
  • 做网站去什么公司商丘网站建设公司
  • 网站设计一般用什么软件广告联盟看广告赚钱
  • 个人网站要买多大的空间中文网站排名
  • 赣州科技有限公司网络优化大师app
  • 建网站做站在百度优化是什么意思
  • 低面效果在哪个网站做无锡百姓网推广