不多说,直接进入我们今天的主题!今天就为大家分享一款最近多人使用的纯HTML代码动画页面源码~
先让我们一起看看效果是怎么样的
是不是特别的炫酷呢!就问你想不想要源代码!现在就立马分享给你们哦!收好了~
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
overflow: hidden;
}
.twitter:hover a {
transform: rotate(-45deg) scale(1.05);
}
.twitter:hover i {
color: #21c2ff;
}
.twitter a {
bottom: -40px;
right: -75px;
transform: rotate(-45deg);
}
.twitter i {
bottom: 7px;
right: 7px;
color: #00ACED;
}
.social-icon a {
position: absolute;
background: white;
color: white;
box-shadow: -1px -1px 20px 0px rgba(0, 0, 0, 0.3);
display: inline-block;
width: 150px;
height: 80px;
transform-origin: 50% 50%;
transition: .15s ease-out;
}
.social-icon i {
position: absolute;
pointer-events: none;
z-index: 1000;
transition: .15s ease-out;
}
.youtube:hover a {
transform: rotate(45deg) scale(1.05);
}
.youtube:hover i {
color: #ec4c44;
}
.youtube a {
bottom: -40px;
left: -75px;
transform: rotate(45deg);
}
.youtube i {
bottom: 7px;
left: 7px;
color: #E62117;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
var canvas = document.querySelector("canvas");
var c = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var mouse = {
x: canvas.width / 2,
y: canvas.height / 2
}
window.addEventListener("resize", function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
window.addEventListener("mousemove", function(e) {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
var colors = [
{r: 255, g: 71, b: 71},
{r: 0, g: 206, b: 237},
{r: 255, g: 255, b: 255}
];
function Particle(x, y, dx, dy, r, ttl) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.r = r;
this.opacity = 1;
this.shouldRemove = false;
this.timeToLive = ttl;
this.randomColor = Math.floor(Math.random() * colors.length);
this.update = function() {
this.x += this.dx
this.y += this.dy
if (this.x + this.r >= canvas.width || this.x - this.r <= 0)
this.dx = -this.dx
if (this.y + this.r >= canvas.height || this.y - this.r <= 0)
this.dy = -this.dy
// Ensure that particles cannot be spawned past canvas boundaries
this.x = Math.min(Math.max(this.x, 0 + this.r), canvas.width - this.r)
this.y = Math.min(Math.max(this.y, 0 + this.r), canvas.height - this.r)
c.beginPath()
c.arc(this.x, this.y, this.r, 0, Math.PI * 2, false)
c.strokeStyle =
'rgba(' +
colors[this.randomColor].r +
',' +
colors[this.randomColor].g +
',' +
colors[this.randomColor].b +
',' +
this.opacity +
')'
c.fillStyle =
'rgba(' +
colors[this.randomColor].r +
',' +
colors[this.randomColor].g +
',' +
colors[this.randomColor].b +
',' +
this.opacity +
')'
c.stroke()
c.closePath()
this.opacity -= 1 / (ttl / 0.1)
this.r -= r / (ttl / 0.1)
if (this.r < 0) this.r = 0 // Thank you Akash for the conditional!
this.timeToLive -= 0.1
}
this.remove = function() {
// If timeToLive expires, return a true value.
// This signifies that the particle should be removed from the scene.
return this.timeToLive <= 0;
}
}
function Explosion(x, y) {
this.particles = [];
this.init = function() {
for (var i = 1; i <= 1; i++) {
var randomVelocity = {
x: (Math.random() - 0.5) * 3.5,
y: (Math.random() - 0.5) * 3.5,
}
this.particles.push(new Particle(x, y, randomVelocity.x, randomVelocity.y, 30, 8));
}
}
this.init();
this.draw = function() {
for (var i = 0; i < this.particles.length; i++) {
this.particles[i].update();
if (this.particles[i].remove() == true) {
this.particles.splice(i, 1);
};
}
}
}
var explosions = [];
function animate() {
window.requestAnimationFrame(animate);
c.fillStyle = "#1e1e1e";
c.fillRect(0, 0, canvas.width, canvas.height);
explosions.push(new Explosion(mouse.x, mouse.y));
for (var i = 0; i < explosions.length; i++) {
explosions[i].draw();
}
}
animate();</script>
</body>
</html>
想要了解更多HTML5的特效,继续关注我哟,或者关注重庆千锋教育官网,学习更多的技术知识。想要学习HTML5技术知识的同学们,也可以到千锋重庆HTML5培训班试听课程。千锋重庆提供两周免费试听课程,欢迎你来试听。
适的动画不仅更能吸引人们的眼球,也能让你的应用体验更为流畅,而将动画的效果做到极致,才能让用户感到使用你的应用是一种享受,而不是觉得生硬和枯燥。那么Web前端人员是否了解各种前端动画效果实现方式的异同,具体应用中又是如何实现的呢?下面就让我们一起来看一看吧~
一、JavaScript 动画
因为没有其它可用的实现方式,最初的前端动画都是JS来实现,实现上就是通过一个定时器setInterval 每隔一定时间来改变元素的样式,动画结束时clearInterval即可。早期的类库包括 jquery、prototype、mootools 等等都是这种方式。
尽管这种方式动画的可控性很强,但是问题也很明显:
· 性能不佳,因为需要不断获取和修改Dom的布局,所以导致了大量页面重排(repaint)
· 缺乏标准,不同的库使用了不同的API,导致即使是简单的动画也有各不相同的实现方式,调整起来比较耗时
· 带宽消耗,相对丰富的动画库代码量都很大,结果就是增加了http请求的大小,降低了页面的载入时间
二、CSS3 动画
css3 加了两种动画的实现方式,一种是 transition, 一种是 animation。
transition 包含4种属性:transition-delay transition-duration transition-property transition-timing-function,对应动画的4种属性: 延迟、持续时间、对应css属性和缓动函数,
transform 包含7种属性:animation-name animation-duration animation-timing-function animation-delay animation-direction animation-iteration-count animation-fill-mode animation-play-state,它们可以定义动画名称,持续时间,缓动函数,动画延迟,动画方向,重复次数,填充模式。
总的来书,css 动画相比与JS更轻量,性能更好,更易于实现,同时也不必担心缺乏标准和增加带宽消耗的问题。animation 相比 transtion 使用起来更为复杂,但也提供了更多的控制,其中最重要的就是 frame 的支持,不过通过一些简单的JS库,例如 TJ 的 move.js, 我们也能在JS中通过 transition 来实现更复杂的控制。
三、Html5 动画
Html5 定义了三种绘图的方式,canvas svg Webgl,其中svg做为一种可缩放矢量图形的实现是基于xml标签定义的,它有专门的 animate 标签来定义动画。而为 canvas 或者 Webgl 实现动画则需要通过 requestAnimationFrame (简称 raf) 来定期刷新画布。尽管说 raf 的方式会让代码变得复杂,但是因为不需要那么多的文档对象(通常浏览器只需要管理一个画布),它的性能也好很多,尤其是在内存吃紧的移动端上面。
通过新的 raf 接口以及一些改进手段我们也可以用JS来实现高性能的动画。主要手段如下:
1. 减少 Dom 样式属性查询,Dom 样式属性的查询会导致页面重排,从而消耗性能,通过将属性保存在JS变量中就可以避免在动画时去查询,从而减少卡顿。
2. 使用性能更好的 css transform 替代改变绝对定位元素的定位属性
3. 在移动设备上使用 3d 硬件加速,最简单办法就是添加 -Webkit-transform: translateZ(0),原因是移动端的显卡有很强的图形渲染能力,而每个应用的 WebvieW 内存却是极其有限的。
使用JS的动画可控性更好,比如说通过事件捕捉可以很容易的设定不同参数。这方面做的最全面的有 Velocity.js,它可做为jquery 插件使用,对于初学者很友好。加入465042726,关于前端方面的更多问题我们可以一起交流!
是一个基于HTML5的齿轮动画特效,我们将齿轮转动的物理学原理,
转换为HTML5代码,在网页上实现了模拟齿轮转动的动画效果。
该齿轮动画的最大特点是它由好多个齿轮组成,
这对齿轮传动的算法要求就大大提高了,而且我们并没有用JavaScript,而是纯CSS3实现的。
HTML代码
<div id="level"> <div id="content"> <div id="gears"> <div id="gears-static"></div> <div id="gear-system-1"> <div class="shadow"id="shadow15"></div> <div id="gear15"></div> <div class="shadow"id="shadow14"></div> <div id="gear14"></div> <div class="shadow"id="shadow13"></div> <div id="gear13"></div> </div> <div id="gear-system-2"> <div class="shadow"id="shadow10"></div> <div id="gear10"></div> <div class="shadow"id="shadow3"></div> <div id="gear3"></div> </div> <div id="gear-system-3"> <div class="shadow"id="shadow9"></div> <div id="gear9"></div> <div class="shadow"id="shadow7"></div> <div id="gear7"></div> </div> <div id="gear-system-4"> <div class="shadow"id="shadow6"></div> <div id="gear6"></div> <div id="gear4"></div> </div> <div id="gear-system-5"> <div class="shadow"id="shadow12"></div> <div id="gear12"></div> <div class="shadow"id="shadow11"></div> <div id="gear11"></div> <div class="shadow"id="shadow8"></div> <div id="gear8"></div> </div> <div class="shadow"id="shadow1"></div> <div id="gear1"></div> <div id="gear-system-6"> <div class="shadow"id="shadow5"></div> <div id="gear5"></div> <div id="gear2"></div> </div> <div class="shadow"id="shadowweight"></div> <div id="chain-circle"></div> <div id="chain"></div> <div id="weight"></div> </div> </div></div>
CSS代码
#level{ width:100%; height:1px; position:absolute; top:50%;}#content{ text-align:center; margin-top:-327px;}#gears{ width:478px; height:655px; position:relative; display:inline-block; vertical-align:middle;}#gears-static{ background:url(FgFnjks.png) no-repeat -363px -903px; width:329px; height:602px; position:absolute; bottom:5px; right:0px; opacity:0.4;}#title{ vertical-align:middle; color:#9EB7B5; width:43%; display:inline-block;}#title h1{ font-family: 'PTSansNarrowBold', sans-serif; font-size:3.6em; text-shadow:rgba(0, 0, 0, 0.36) 7px7px10px;}#title p{ font-family: sans-serif; font-size:1.2em; line-height:148%; text-shadow:rgba(0, 0, 0, 0.36) 1px1px0px;} .shadow{ -webkit-box-shadow: 4px7px25px10pxrgba(43, 36, 0, 0.36); -moz-box-shadow: 4px7px25px10pxrgba(43, 36, 0, 0.36); box-shadow: 4px7px25px10pxrgba(43, 36, 0, 0.36);} /*gear-system-1*/#gear15{ background: url(FgFnjks.png) no-repeat 0 -993px; width: 321px; height: 321px; position:absolute; left:45px; top:179px; -webkit-animation: rotate-back 24000ms linear infinite; -moz-animation: rotate-back 24000ms linear infinite; -ms-animation: rotate-back 24000ms linear infinite; animation: rotate-back 24000ms linear infinite;}#shadow15{ width:306px; height:306px; -webkit-border-radius:153px; -moz-border-radius:153px; border-radius:153px; position:absolute; left:52px; top:186px;}#gear14{ background: url(FgFnjks.png) no-repeat 0 -856px; width: 87px; height: 87px; position:absolute; left:162px; top:296px;}#shadow14{ width:70px; height:70px; -webkit-border-radius:35px; -moz-border-radius:35px; border-radius:35px; position:absolute; left:171px; top:304px;}#gear13{ background: url(FgFnjks.png) no-repeat 0 -744px; width: 62px; height: 62px; position:absolute; left:174px; top:309px; -webkit-animation: rotate 8000ms linear infinite; -moz-animation: rotate 8000ms linear infinite; -ms-animation: rotate 8000ms linear infinite; animation: rotate 8000ms linear infinite;}#shadow13{ width:36px; height:36px; -webkit-border-radius:18px; -moz-border-radius:18px; border-radius:18px; position:absolute; left:187px; top:322px;} /*gear-system-2*/#gear10{ background: url(FgFnjks.png) no-repeat 0 -184px; width: 122px; height: 122px; position:absolute; left:175px; top:0; -webkit-animation: rotate-back 8000ms linear infinite; -moz-animation: rotate-back 8000ms linear infinite; -ms-animation: rotate-back 8000ms linear infinite; animation: rotate-back 8000ms linear infinite;}#shadow10{ width:86px; height:86px; -webkit-border-radius:43px; -moz-border-radius:43px; border-radius:43px; position:absolute; left:193px; top:18px;}#gear3{ background: url(FgFnjks.png) no-repeat 0 -1493px; width: 85px; height: 84px; position:absolute; left:194px; top:19px; -webkit-animation: rotate 10000ms linear infinite; -moz-animation: rotate 10000ms linear infinite; -ms-animation: rotate 10000ms linear infinite; animation: rotate 10000ms linear infinite;}#shadow3{ width:60px; height:60px; -webkit-border-radius:30px; -moz-border-radius:30px; border-radius:30px; position:absolute; left:206px; top:31px;} /*gear-system-3*/#gear9{ background: url(FgFnjks.png) no-repeat -371px -280px; width: 234px; height: 234px; position:absolute; left:197px; top:96px; -webkit-animation: rotate 12000ms linear infinite; -moz-animation: rotate 12000ms linear infinite; -ms-animation: rotate 12000ms linear infinite; animation: rotate 12000ms linear infinite;}#shadow9{ width:200px; height:200px; -webkit-border-radius:100px; -moz-border-radius:100px; border-radius:100px; position:absolute; left:214px; top:113px;}#gear7{ background: url(FgFnjks.png) no-repeat -371px0; width: 108px; height: 108px; position:absolute; left:260px; top:159px; -webkit-animation: rotate-back 10000ms linear infinite; -moz-animation: rotate-back 10000ms linear infinite; -ms-animation: rotate-back 10000ms linear infinite; animation: rotate-back 10000ms linear infinite;}#shadow7{ width:76px; height:76px; -webkit-border-radius:38px; -moz-border-radius: 38px; border-radius: 38px; position:absolute; left:276px; top:175px;} /*gear-system-4*/#gear6{ background: url(FgFnjks.png) no-repeat 0 -1931px; width: 134px; height: 134px; position:absolute; left:305px; bottom:212px; -webkit-animation: rotate-back 10000ms linear infinite; -moz-animation: rotate-back 10000ms linear infinite; -ms-animation: rotate-back 10000ms linear infinite; animation: rotate-back 10000ms linear infinite;}#shadow6{ width:98px; height:98px; -webkit-border-radius:49px; -moz-border-radius: 49px; border-radius: 49px; position:absolute; left:323px; bottom:230px;}#gear4{ background: url(FgFnjks.png) no-repeat 0 -1627px; width: 69px; height: 69px; position:absolute; left:337px; bottom:245px; -webkit-animation: rotate-back 10000ms linear infinite; -moz-animation: rotate-back 10000ms linear infinite; -ms-animation: rotate-back 10000ms linear infinite; animation: rotate-back 10000ms linear infinite;} /*gear-system-5*/#gear12{ background: url(FgFnjks.png) no-repeat 0 -530px; width: 164px;
*请认真填写需求信息,我们会在24小时内与您取得联系。