anime.js网页背景动画特效代码

版权:原创 更新时间:1年以上
[该文章底部包含文件资源,可根据自己情况,决定是否下载资源使用,时间>金钱,如有需要,立即查看资源]

以下是 anime.js网页背景动画特效代码 的示例演示效果:

当前平台(PC电脑)
  • 平台:

部分效果截图:

anime.js网页背景动画特效代码

HTML代码(index.html):

<!DOCTYPE html>
<html>
<head>
<title>anime.js</title>
<meta charset="utf-8">
<link rel="icon" type="image/png" href="img/favicon.png" />
<link rel="apple-touch-icon" href="img/apple-touch-icon.png" />
<link href="css/styles.css" rel="stylesheet">
</head>
<style>

:root { font-size: 14px; }
@media (min-width: 350px) { :root { font-size: 16px; } }
@media (min-width: 640px) { :root { font-size: 18px; } }
@media (min-width: 1440px) { :root { font-size: 20px; } }

section {
position: relative;
z-index: 1;
width: 18.5rem;
}

.description {
line-height: 1.25rem;
}

.links {
line-height: 1.125rem;
}

.description,
.links {
opacity: 0;
margin-bottom: 1.25rem;
}

.links {
display: flex;
justify-content: space-between;
width: 100%;
height: auto;
}

.link {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 8.75rem;
height: 3.5rem;
border: 1px solid #3A5685;
border-radius: 2px;
color: white;
text-decoration: none;
text-align: center;
transition: border-color .100s ease, background-color .100s ease;
}

.link:hover {
border-color: #647899;
background: rgba(255,255,255,.06);
}

.link.primary {
border: none;
background-image: linear-gradient(-180deg, #2796F8 0%, #206EFF 100%);
}

.link.primary:hover {
border: none;
background: linear-gradient(-180deg, #4DA7F7 0%, #3577F3 100%);
}

.small {
font-size: .75rem;
}

/* Logo */

.logo {
opacity: 0;
position: relative;
margin-left: -3.5rem;
margin-top: -8rem;
margin-bottom: .75rem;
font-size: 8px;
}

body.ready .logo {
opacity: 1;
}

body:not(.iOS) #lines * {
mix-blend-mode: lighten;
}

#fills * {
opacity: 0;
mix-blend-mode: lighten;
}

#line-i-1 {
transform-origin: 30em 8em;
}

.fireworks {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
cursor: pointer;
}

footer {
opacity: 0;
color: #96A9C3;
}

footer a {
color: #96A9C3;
text-decoration: none;
}

footer a:hover {
color: #FFF;
text-decoration: underline;
}

</style>
<body>
<canvas class="fireworks"></canvas>
<section>
<svg class="logo" width="25rem" height="12rem" viewBox="0 0 800 384">
  <defs>
	<radialGradient cx="50%" cy="0%" fx="50%" fy="0%" r="50%" id="radialGradient-1">
	  <stop stop-color="#329FFF" offset="0%"></stop>
	  <stop stop-color="#206EFF" offset="100%"></stop>
	</radialGradient>
	<radialGradient cx="50%" cy="0%" fx="50%" fy="0%" r="50%" id="radialGradient-2">
	  <stop stop-color="#FF7894" offset="0%"></stop>
	  <stop stop-color="#FF324A" offset="100%"></stop>
	</radialGradient>
	<radialGradient cx="50%" cy="0%" fx="50%" fy="0%" r="100%" id="radialGradient-3">
	  <stop stop-color="#FF7894" offset="0%"></stop>
	  <stop stop-color="#FF324A" offset="100%"></stop>
	</radialGradient>
	<radialGradient cx="50%" cy="0%" fx="50%" fy="0%" r="100%" id="radialGradient-4">
	  <stop stop-color="#359FFC" offset="0%"></stop>
	  <stop stop-color="#206EFF" offset="100%"></stop>
	</radialGradient>
	<radialGradient cx="50%" cy="0%" fx="50%" fy="0%" r="50%" id="radialGradient-5">
	  <stop stop-color="#5FFFD2" offset="0%"></stop>
	  <stop stop-color="#31FFA6" offset="100%"></stop>
	</radialGradient>
  </defs>
  <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
	<rect id="dot-js" fill="#FFFFFF" x="80" y="352" width="32" height="32" rx="16"></rect>
	<g id="lines" transform="translate(128.000000, 256.000000)">
	  <path d="M531.599976,119.042822 C531.872599,121.442657 532.736164,123.612749 534.213875,124.965578 C535.547208,126.186231 537.324986,127 539.587612,127 C542.133067,127 544.032057,126.104854 545.36539,124.680759 C546.739127,123.175287 547.506804,120.978112 547.506804,118.374052 L547.506804,97" id="line-s" stroke="#FDFDFD" stroke-linecap="square"></path>
	  <path d="M576.512399,105.204082 C576.353483,103.122449 575.558903,101.612245 574.367034,100.183673 C572.777876,98.2653061 569.758474,97 566.739073,97 C564.116961,97 561.534578,97.8979592 559.786504,99.5306122 C558.356261,100.877551 557.323308,102.591837 557.323308,105 C557.323308,108.410588 560.43582,110.318694 563,110.738061 C566,111.228706 568.5,111.228706 572,112.209996 C574.445972,112.89577 577.108333,115.626631 577.108333,118.632653 C577.108333,121.163265 576.194567,122.836735 574.883511,124.183673 C572.976521,126.102041 570.155764,127 566.897989,127 C563.679942,127 560.898915,125.938776 559.031653,123.897959 C557.641139,122.387755 556.687644,120.346939 556.488999,118.142857" id="line-j" stroke="#FDFDFD"></path>
	  <path d="M48,112 C21.490332,112 0,90.509668 0,64 C0,37.490332 21.490332,16 48,16 C74.509668,16 96,37.490332 96,64 L96,128" id="line-a" stroke="#206EFF" stroke-width="32"></path>
	  <path d="M96,0 L96,128 L96,64 C96,37.490332 117.490332,16 144,16 C170.509668,16 192,37.490332 192,64 L192,128" id="line-n-1" stroke="#FF324A" stroke-width="32"></path>
	  <path d="M224,0 L224,128 L224,64 C224,37.490332 245.490332,16 272,16 C298.509668,16 320,37.490332 320,64 L320,128" id="line-m-1" stroke="#206EFF" stroke-width="32"></path>
	  <path d="M320,128 L320,64 C320,37.490332 341.490332,16 368,16 C394.509668,16 416,37.490332 416,64 L416,128" id="line-m-2" stroke="#FF324A" stroke-width="32"></path>
	  <path d="M192,48 L192,64 C192,90.509668 213.490332,112 240,112" id="line-i-1" stroke="#31FFA6" stroke-width="32"></path>
	  <path d="M464,80 L512,80 L512,64 C512,37.490332 490.509668,16 464,16 C437.490332,16 416,37.490332 416,64 C416,90.509668 437.490332,112 464,112" id="line-e" stroke="#31FFA6" stroke-width="32"></path>
	</g>
	<g id="fills" transform="translate(112.000000, 256.000000)">
	  <path d="M64,32 C46.326888,32 32,46.326888 32,64 C32,81.673112 46.326888,96 64,96 L64,128 C28.653776,128 0,99.346224 0,64 C0,28.653776 28.653776,0 64,0 C99.346224,0 128,28.653776 128,64 L128,128 L96,128 L96,64 C96,46.326888 81.673112,32 64,32 Z" id="fill-a" fill="url(#radialGradient-1)"></path>
	  <path d="M192,64 C192,46.326888 177.673112,32 160,32 C142.326888,32 128,46.326888 128,64 L128,128 L96,128 L96,64 C96,28.653776 124.653776,0 160,0 C195.346224,0 224,28.653776 224,64 L224,128 L192,128 L192,64" id="fill-n-2" fill="url(#radialGradient-2)"></path>
	  <rect id="fill-n-1" fill="url(#radialGradient-3)" x="96" y="0" width="32" height="128"></rect>
	  <path d="M256,64 C256,46.326888 270.326888,32 288,32 C305.673112,32 320,46.326888 320,64 L320,128 L352,128 L352,64 C352,28.653776 323.346224,0 288,0 C252.653776,0 224,28.653776 224,64 L256,64 L256,64 Z" id="fill-m-2" fill="url(#radialGradient-1)"></path>
	  <rect id="fill-m-1" fill="url(#radialGradient-4)" x="224" y="0" width="32" height="128"></rect>
	  <path d="M416,64 C416,46.326888 401.673112,32 384,32 C366.326888,32 352,46.326888 352,64 L352,128 L320,128 L320,64 C320,28.653776 348.653776,0 384,0 C419.346224,0 448,28.653776 448,64 L448,128 L416,128 L416,64" id="fill-m-3" fill="url(#radialGradient-2)"></path>
	  <path d="M224,64 C224,81.673112 238.326888,96 256,96 L256,128 C220.653776,128 192,99.346224 192,64 L192,48 L192,48 L224,48 L224,64" id="fill-i-1" fill="url(#radialGradient-5)"></path>
	  <circle id="dot-i" fill="url(#radialGradient-5)" cx="208" cy="16" r="16"></circle>
	  <path d="M512,64 C512,46.326888 497.673112,32 480,32 L480,32 C462.326888,32 448,46.326888 448,64 C448,81.673112 462.326888,96 480,96 L480,128 C444.653776,128 416,99.346224 416,64 C416,28.653776 444.653776,0 480,0 C515.346224,0 544,28.653776 544,64 L544,96 L512,96 L480,96 L480,64 L512,64" id="fill-e-1" fill="url(#radialGradient-5)"></path>
	</g>
  </g>
</svg>

</section>

<script src="anime.js"></script>
<script>
var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
var ff = navigator.userAgent.indexOf('Firefox') > 0;
var tap = ('ontouchstart' in window || navigator.msMaxTouchPoints) ? 'touchstart' : 'mousedown';
if (iOS) document.body.classList.add('iOS');

var fireworks = (function() {

  var getFontSize = function() {
	return parseFloat(getComputedStyle(document.documentElement).fontSize);
  }

  var canvas = document.querySelector('.fireworks');
  var ctx = canvas.getContext('2d');
  var numberOfParticules = 24;
  var distance = 200;
  var x = 0;
  var y = 0;
  var animations = [];

  var setCanvasSize = function() {
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
  }

  var updateCoords = function(e) {
	x = e.clientX || e.touches[0].clientX;
	y = e.clientY || e.touches[0].clientY;
  }

  var colors = ['#FF324A', '#31FFA6', '#206EFF', '#FFFF99'];

  var createCircle = function(x,y) {
	var p = {};
	p.x = x;
	p.y = y;
	p.color = colors[anime.random(0, colors.length - 1)];
	p.color = '#FFF';
	p.radius = 0;
	p.alpha = 1;
	p.lineWidth = 6;
	p.draw = function() {
	  ctx.globalAlpha = p.alpha;
	  ctx.beginPath();
	  ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);
	  ctx.lineWidth = p.lineWidth;
	  ctx.strokeStyle = p.color;
	  ctx.stroke();
	  ctx.globalAlpha = 1;
	}
	return p;
  }

  var createParticule = function(x,y) {
	var p = {};
	p.x = x;
	p.y = y;
	p.color = colors[anime.random(0, colors.length - 1)];
	p.radius = anime.random(getFontSize(), getFontSize() * 2);
	p.draw = function() {
	  ctx.beginPath();
	  ctx.arc(p.x, p.y, p.radius, 0, 2 * Math.PI, true);
	  ctx.fillStyle = p.color;
	  ctx.fill();
	}
	return p;
  }

  var createParticles = function(x,y) {
	var particules = [];
	for (var i = 0; i < numberOfParticules; i++) {
	  var p = createParticule(x, y);
	  particules.push(p);
	}
	return particules;
  }

  var removeAnimation = function(animation) {
	var index = animations.indexOf(animation);
	if (index > -1) animations.splice(index, 1);
  }

  var animateParticules = function(x, y) {
	setCanvasSize();
	var particules = createParticles(x, y);
	var circle = createCircle(x, y);
	var particulesAnimation = anime({
	  targets: particules,
	  x: function(p) { return p.x + anime.random(-distance, distance); },
	  y: function(p) { return p.y + anime.random(-distance, distance); },
	  radius: 0,
	  duration: function() { return anime.random(1200, 1800); },
	  easing: 'easeOutExpo',
	  complete: removeAnimation
	});
	var circleAnimation = anime({
	  targets: circle,
	  radius: function() { return anime.random(getFontSize() * 8.75, getFontSize() * 11.25); },
	  lineWidth: 0,
	  alpha: {
		value: 0,
		easing: 'linear',
		duration: function() { return anime.random(400, 600); }
	  },
	  duration: function() { return anime.random(1200, 1800); },
	  easing: 'easeOutExpo',
	  complete: removeAnimation
	});
	animations.push(particulesAnimation);
	animations.push(circleAnimation);
  }

  var mainLoop = anime({
	duration: Infinity,
	update: function() {
	  ctx.clearRect(0, 0, canvas.width, canvas.height);
	  animations.forEach(function(anim) {
		anim.animatables.forEach(function(animatable) {
		  animatable.target.draw();
		});
	  });
	}
  });

  document.addEventListener(tap, function(e) {
	updateCoords(e);
	animateParticules(x, y);
	ga('send', 'event', 'Fireworks', 'Click');
  }, false);

  window.addEventListener('resize', setCanvasSize, false);

  return {
	boom: animateParticules
  }

})();

var logoAnimation = function() {

  document.body.classList.add('ready');

  var setDashoffset = function(el) {
	var l = el.getTotalLength();
	el.setAttribute('stroke-dasharray', l);
	return [l,0];
  }

  var letters = anime({
	targets: '#lines path',
	strokeDashoffset: {
	  value: setDashoffset,
	  duration: 700,
	  easing: 'easeOutQuad'
	},
	transform: ['translate(0 128)', 'translate(0 0)'],
	delay: function(el, i) {
	  return 750 + (i * 120)
	},
	duration: 1400
  });

  var dotJSRoll = anime({
	targets: '#dot-js',
	transform: ['translate(0 0)', 'translate(544 0)'],
	delay: letters.duration - 800,
	duration: 800,
	elasticity: 300
  });

  var dotJSDown = anime({
	targets: '#dot-js',
	transform: ['translate(0 -304)', 'translate(0 0)'],
	duration: 500,
	elasticity: 600,
	autoplay: false
  });

  var dotJSUp = anime({
	targets: '#dot-js',
	transform: ['translate(0 0) scale(1 3)', 'translate(0 -352) scale(1 1)'],
	duration: 800,
	easing: 'easeOutCirc',
	complete: dotJSDown.play
  });

  var boom = anime({
	duration: 880,
	complete: function(a) {
	  var dot = dotJSDown.animatables[0].target.getBoundingClientRect();
	  var pos = {x: dot.left + (dot.width / 2), y: dot.top + (dot.height / 2)}
	  fireworks.boom(pos.x, pos.y);
	}
  });

  var letterI = anime({
	targets: '#line-i-1',
	strokeDashoffset: {
	  value: setDashoffset,
	  duration: 700,
	  easing: 'easeOutQuad'
	},
	transform: function() {
	  return ff ? ['rotate(360)', 'rotate(0)'] : ['rotate(360 240 64)', 'rotate(0 240 64)'];
	},
	duration: 2500,
	delay: letters.duration - 780
  });

  var dotI = anime({
	targets: '#dot-i',
	transform: ['translate(0 -352) scale(1 3)', 'translate(0 0) scale(1 1)'],
	opacity: {
	  value: [0, 1],
	  easing: 'linear',
	  duration: 100
	},
	delay: letters.duration + 250
  });

  var JSletters = anime({
	targets: ['#line-j', '#line-s'],
	strokeDashoffset: setDashoffset,
	duration: 1400,
	delay: function(el, i) { return (letterI.duration - 1400) + (i * 60) },
	easing: 'easeInOutQuart'
  });

  var gradients = anime({
	targets: '#fills *:not(#dot-i)',
	opacity: [0, 1],
	delay: letterI.duration - 300,
	delay: function(el, i, l) {
	  var mid = l/2;
	  var index = (i - mid) > mid ? 0 : i;
	  var delay = Math.abs(index - mid);
	  return (letterI.duration - 1300) + (delay * 30);
	},
	duration: 500,
	easing: 'linear'
  });

  var showDescription = anime({
	targets: ['.logo', '.description', '.links', 'footer'],
	opacity: {
	  value: 1,
	  easing: 'linear',
	  duration: 1000
	},
	translateY: ['1.5rem', '0rem'],
	delay: function(el, i, l) { return ((l - i) * 100) + (letterI.duration - 600); },
	duration: 2250,
	easing: 'easeOutExpo',
  });

}
window.onload = logoAnimation;
</script>
</body>
</html>

JS代码(anime.js):

/* * Anime v1.1.2 * http://anime-js.com * JavaScript animation engine * Copyright (c) 2016 Julian Garnier * http://juliangarnier.com * Released under the MIT license */
(function (root,factory){
	if (typeof define === 'function' && define.amd){
	// AMD. Register as an anonymous module. define([],factory);
}
else if (typeof module === 'object' && module.exports){
	// Node. Does not work with strict CommonJS,but // only CommonJS-like environments that support module.exports,// like Node. module.exports = factory();
}
else{
	// Browser globals (root is window) root.anime = factory();
}
}
(this,function (){
	var version = '1.1.2';
	// Defaults var defaultSettings ={
	duration:1000,delay:0,loop:false,autoplay:true,direction:'normal',easing:'easeOutElastic',elasticity:400,round:false,begin:undefined,update:undefined,complete:undefined}
// Transforms var validTransforms = ['translateX','translateY','translateZ','rotate','rotateX','rotateY','rotateZ','scale','scaleX','scaleY','scaleZ','skewX','skewY'];
	var transform,transformStr = 'transform';
	// Utils var is ={
	arr:function(a){
	return Array.isArray(a)}
,obj:function(a){
	return Object.prototype.toString.call(a).indexOf('Object') > -1}
,svg:function(a){
	return a instanceof SVGElement}
,dom:function(a){
	return a.nodeType || is.svg(a)}
,num:function(a){
	return !isNaN(parseInt(a))}
,str:function(a){
	return typeof a === 'string'}
,fnc:function(a){
	return typeof a === 'function'}
,und:function(a){
	return typeof a === 'undefined'}
,nul:function(a){
	return typeof a === 'null'}
,hex:function(a){
	return /(^#[0-9A-F]{
	6}
$)|(^#[0-9A-F]{
	3}
$)/i.test(a)}
,rgb:function(a){
	return /^rgb/.test(a)}
,hsl:function(a){
	return /^hsl/.test(a)}
,col:function(a){
	return (is.hex(a) || is.rgb(a) || is.hsl(a))}
}
// Easings functions adapted from http://jqueryui.com/ var easings = (function(){
	var eases ={
}
;
	var names = ['Quad','Cubic','Quart','Quint','Expo'];
	var functions ={
	Sine:function(t){
	return 1 - Math.cos( t * Math.PI / 2 );
}
,Circ:function(t){
	return 1 - Math.sqrt( 1 - t * t );
}
,Elastic:function(t,m){
	if( t === 0 || t === 1 ) return t;
	var p = (1 - Math.min(m,998) / 1000),st = t / 1,st1 = st - 1,s = p / ( 2 * Math.PI ) * Math.asin( 1 );
	return -( Math.pow( 2,10 * st1 ) * Math.sin( ( st1 - s ) * ( 2 * Math.PI ) / p ) );
}
,Back:function(t){
	return t * t * ( 3 * t - 2 );
}
,Bounce:function(t){
	var pow2,bounce = 4;
	while ( t < ( ( pow2 = Math.pow( 2,--bounce ) ) - 1 ) / 11 ){
}
return 1 / Math.pow( 4,3 - bounce ) - 7.5625 * Math.pow( ( pow2 * 3 - 2 ) / 22 - t,2 );
}
}
names.forEach(function(name,i){
	functions[name] = function(t){
	return Math.pow( t,i + 2 );
}
}
);
	Object.keys(functions).forEach(function(name){
	var easeIn = functions[name];
	eases['easeIn' + name] = easeIn;
	eases['easeOut' + name] = function(t,m){
	return 1 - easeIn(1 - t,m);
}
;
	eases['easeInOut' + name] = function(t,m){
	return t < 0.5 ? easeIn(t * 2,m) / 2:1 - easeIn(t * -2 + 2,m) / 2;
}
;
	eases['easeOutIn' + name] = function(t,m){
	return t < 0.5 ? (1 - easeIn(1 - 2 * t,m)) / 2:(easeIn(t * 2 - 1,m) + 1) / 2;
}
;
}
);
	eases.linear = function(t){
	return t;
}
;
	return eases;
}
)();
	// Strings var numberToString = function(val){
	return (is.str(val)) ? val:val + '';
}
var stringToHyphens = function(str){
	return str.replace(/([a-z])([A-Z])/g,'$1-$2').toLowerCase();
}
var selectString = function(str){
	if (is.col(str)) return false;
	try{
	var nodes = document.querySelectorAll(str);
	return nodes;
}
catch(e){
	return false;
}
}
// Numbers var random = function(min,max){
	return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Arrays var flattenArray = function(arr){
	return arr.reduce(function(a,b){
	return a.concat(is.arr(b) ? flattenArray(b):b);
}
,[]);
}
var toArray = function(o){
	if (is.arr(o)) return o;
	if (is.str(o)) o = selectString(o) || o;
	if (o instanceof NodeList || o instanceof HTMLCollection) return [].slice.call(o);
	return [o];
}
var arrayContains = function(arr,val){
	return arr.some(function(a){
	return a === val;
}
);
}
var groupArrayByProps = function(arr,propsArr){
	var groups ={
}
;
	arr.forEach(function(o){
	var group = JSON.stringify(propsArr.map(function(p){
	return o[p];
}
));
	groups[group] = groups[group] || [];
	groups[group].push(o);
}
);
	return Object.keys(groups).map(function(group){
	return groups[group];
}
);
}
var removeArrayDuplicates = function(arr){
	return arr.filter(function(item,pos,self){
	return self.indexOf(item) === pos;
}
);
}
// Objects var cloneObject = function(o){
	var newObject ={
}
;
	for (var p in o) newObject[p] = o[p];
	return newObject;
}
var mergeObjects = function(o1,o2){
	for (var p in o2) o1[p] = !is.und(o1[p]) ? o1[p]:o2[p];
	return o1;
}
// Colors var hexToRgb = function(hex){
	var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
	var hex = hex.replace(rgx,function(m,r,g,b){
	return r + r + g + g + b + b;
}
);
	var rgb = /^#?([a-f\d]{
	2}
)([a-f\d]{
	2}
)([a-f\d]{
	2}
)$/i.exec(hex);
	var r = parseInt(rgb[1],16);
	var g = parseInt(rgb[2],16);
	var b = parseInt(rgb[3],16);
	return 'rgb(' + r + ',' + g + ',' + b + ')';
}
var hslToRgb = function(hsl){
	var hsl = /hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)/g.exec(hsl);
	var h = parseInt(hsl[1]) / 360;
	var s = parseInt(hsl[2]) / 100;
	var l = parseInt(hsl[3]) / 100;
	var hue2rgb = function(p,q,t){
	if (t < 0) t += 1;
	if (t > 1) t -= 1;
	if (t < 1/6) return p + (q - p) * 6 * t;
	if (t < 1/2) return q;
	if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
	return p;
}
var r,g,b;
	if (s == 0){
	r = g = b = l;
}
else{
	var q = l < 0.5 ? l * (1 + s):l + s - l * s;
	var p = 2 * l - q;
	r = hue2rgb(p,q,h + 1/3);
	g = hue2rgb(p,q,h);
	b = hue2rgb(p,q,h - 1/3);
}
return 'rgb(' + r * 255 + ',' + g * 255 + ',' + b * 255 + ')';
}
var colorToRgb = function(val){
	if (is.rgb(val)) return val;
	if (is.hex(val)) return hexToRgb(val);
	if (is.hsl(val)) return hslToRgb(val);
}
// Units var getUnit = function(val){
	return /([\+\-]?[0-9|auto\.]+)(%|px|pt|em|rem|in|cm|mm|ex|pc|vw|vh|deg)?/.exec(val)[2];
}
var addDefaultTransformUnit = function(prop,val,intialVal){
	if (getUnit(val)) return val;
	if (prop.indexOf('translate') > -1) return getUnit(intialVal) ? val + getUnit(intialVal):val + 'px';
	if (prop.indexOf('rotate') > -1 || prop.indexOf('skew') > -1) return val + 'deg';
	return val;
}
// Values var getCSSValue = function(el,prop){
	// First check if prop is a valid CSS property if (prop in el.style){
	// Then return the property value or fallback to '0' when getPropertyValue fails return getComputedStyle(el).getPropertyValue(stringToHyphens(prop)) || '0';
}
}
var getTransformValue = function(el,prop){
	var defaultVal = prop.indexOf('scale') > -1 ? 1:0;
	var str = el.style.transform;
	if (!str) return defaultVal;
	var rgx = /(\w+)\((.+?)\)/g;
	var match = [];
	var props = [];
	var values = [];
	while (match = rgx.exec(str)){
	props.push(match[1]);
	values.push(match[2]);
}
var val = values.filter(function(f,i){
	return props[i] === prop;
}
);
	return val.length ? val[0]:defaultVal;
}
var getAnimationType = function(el,prop){
	if ( is.dom(el) && arrayContains(validTransforms,prop)) return 'transform';
	if ( is.dom(el) && (el.getAttribute(prop) || (is.svg(el) && el[prop]))) return 'attribute';
	if ( is.dom(el) && (prop !== 'transform' && getCSSValue(el,prop))) return 'css';
	if (!is.nul(el[prop]) && !is.und(el[prop])) return 'object';
}
var getInitialTargetValue = function(target,prop){
	switch (getAnimationType(target,prop)){
	case 'transform':return getTransformValue(target,prop);
	case 'css':return getCSSValue(target,prop);
	case 'attribute':return target.getAttribute(prop);
}
return target[prop] || 0;
}
var getValidValue = function(values,val,originalCSS){
	if (is.col(val)) return colorToRgb(val);
	if (getUnit(val)) return val;
	var unit = getUnit(values.to) ? getUnit(values.to):getUnit(values.from);
	if (!unit && originalCSS) unit = getUnit(originalCSS);
	return unit ? val + unit:val;
}
var decomposeValue = function(val){
	var rgx = /-?\d*\.?\d+/g;
	return{
	original:val,numbers:numberToString(val).match(rgx) ? numberToString(val).match(rgx).map(Number):[0],strings:numberToString(val).split(rgx)}
}
var recomposeValue = function(numbers,strings,initialStrings){
	return strings.reduce(function(a,b,i){
	var b = (b ? b:initialStrings[i - 1]);
	return a + numbers[i - 1] + b;
}
);
}
// Animatables var getAnimatables = function(targets){
	var targets = targets ? (flattenArray(is.arr(targets) ? targets.map(toArray):toArray(targets))):[];
	return targets.map(function(t,i){
	return{
	target:t,id:i}
;
}
);
}
// Properties var getProperties = function(params,settings){
	var props = [];
	for (var p in params){
	if (!defaultSettings.hasOwnProperty(p) && p !== 'targets'){
	var prop = is.obj(params[p]) ? cloneObject(params[p]):{
	value:params[p]}
;
	prop.name = p;
	props.push(mergeObjects(prop,settings));
}
}
return props;
}
var getPropertiesValues = function(target,prop,value,i){
	var values = toArray( is.fnc(value) ? value(target,i):value);
	return{
	from:(values.length > 1) ? values[0]:getInitialTargetValue(target,prop),to:(values.length > 1) ? values[1]:values[0]}
}
// Tweens var getTweenValues = function(prop,values,type,target){
	var valid ={
}
;
	if (type === 'transform'){
	valid.from = prop + '(' + addDefaultTransformUnit(prop,values.from,values.to) + ')';
	valid.to = prop + '(' + addDefaultTransformUnit(prop,values.to) + ')';
}
else{
	var originalCSS = (type === 'css') ? getCSSValue(target,prop):undefined;
	valid.from = getValidValue(values,values.from,originalCSS);
	valid.to = getValidValue(values,values.to,originalCSS);
}
return{
	from:decomposeValue(valid.from),to:decomposeValue(valid.to)}
;
}
var getTweensProps = function(animatables,props){
	var tweensProps = [];
	animatables.forEach(function(animatable,i){
	var target = animatable.target;
	return props.forEach(function(prop){
	var animType = getAnimationType(target,prop.name);
	if (animType){
	var values = getPropertiesValues(target,prop.name,prop.value,i);
	var tween = cloneObject(prop);
	tween.animatables = animatable;
	tween.type = animType;
	tween.from = getTweenValues(prop.name,values,tween.type,target).from;
	tween.to = getTweenValues(prop.name,values,tween.type,target).to;
	tween.round = (is.col(values.from) || tween.round) ? 1:0;
	tween.delay = (is.fnc(tween.delay) ? tween.delay(target,i,animatables.length):tween.delay) / animation.speed;
	tween.duration = (is.fnc(tween.duration) ? tween.duration(target,i,animatables.length):tween.duration) / animation.speed;
	tweensProps.push(tween);
}
}
);
}
);
	return tweensProps;
}
var getTweens = function(animatables,props){
	var tweensProps = getTweensProps(animatables,props);
	var splittedProps = groupArrayByProps(tweensProps,['name','from','to','delay','duration']);
	return splittedProps.map(function(tweenProps){
	var tween = cloneObject(tweenProps[0]);
	tween.animatables = tweenProps.map(function(p){
	return p.animatables}
);
	tween.totalDuration = tween.delay + tween.duration;
	return tween;
}
);
}
var reverseTweens = function(anim,delays){
	anim.tweens.forEach(function(tween){
	var toVal = tween.to;
	var fromVal = tween.from;
	var delayVal = anim.duration - (tween.delay + tween.duration);
	tween.from = toVal;
	tween.to = fromVal;
	if (delays) tween.delay = delayVal;
}
);
	anim.reversed = anim.reversed ? false:true;
}
var getTweensDuration = function(tweens){
	if (tweens.length) return Math.max.apply(Math,tweens.map(function(tween){
	return tween.totalDuration;
}
));
}
var getTweensDelay = function(tweens){
	if (tweens.length) return Math.min.apply(Math,tweens.map(function(tween){
	return tween.delay;
}
));
}
// will-change var getWillChange = function(anim){
	var props = [];
	var els = [];
	anim.tweens.forEach(function(tween){
	if (tween.type === 'css' || tween.type === 'transform' ){
	props.push(tween.type === 'css' ? stringToHyphens(tween.name):'transform');
	tween.animatables.forEach(function(animatable){
	els.push(animatable.target);
}
);
}
}
);
	return{
	properties:removeArrayDuplicates(props).join(','),elements:removeArrayDuplicates(els)}
}
var setWillChange = function(anim){
	var willChange = getWillChange(anim);
	willChange.elements.forEach(function(element){
	element.style.willChange = willChange.properties;
}
);
}
var removeWillChange = function(anim){
	var willChange = getWillChange(anim);
	willChange.elements.forEach(function(element){
	element.style.removeProperty('will-change');
}
);
}
/* Svg path */
 var getPathProps = function(path){
	var el = is.str(path) ? selectString(path)[0]:path;
	return{
	path:el,value:el.getTotalLength()}
}
var snapProgressToPath = function(tween,progress){
	var pathEl = tween.path;
	var pathProgress = tween.value * progress;
	var point = function(offset){
	var o = offset || 0;
	var p = progress > 1 ? tween.value + o:pathProgress + o;
	return pathEl.getPointAtLength(p);
}
var p = point();
	var p0 = point(-1);
	var p1 = point(+1);
	switch (tween.name){
	case 'translateX':return p.x;
	case 'translateY':return p.y;
	case 'rotate':return Math.atan2(p1.y - p0.y,p1.x - p0.x) * 180 / Math.PI;
}
}
// Progress var getTweenProgress = function(tween,time){
	var elapsed = Math.min(Math.max(time - tween.delay,0),tween.duration);
	var percent = elapsed / tween.duration;
	var progress = tween.to.numbers.map(function(number,p){
	var start = tween.from.numbers[p];
	var eased = easings[tween.easing](percent,tween.elasticity);
	var val = tween.path ? snapProgressToPath(tween,eased):start + eased * (number - start);
	val = tween.round ? Math.round(val * tween.round) / tween.round:val;
	return val;
}
);
	return recomposeValue(progress,tween.to.strings,tween.from.strings);
}
var setAnimationProgress = function(anim,time){
	var transforms;
	anim.currentTime = time;
	anim.progress = (time / anim.duration) * 100;
	for (var t = 0;
	t < anim.tweens.length;
	t++){
	var tween = anim.tweens[t];
	tween.currentValue = getTweenProgress(tween,time);
	var progress = tween.currentValue;
	for (var a = 0;
	a < tween.animatables.length;
	a++){
	var animatable = tween.animatables[a];
	var id = animatable.id;
	var target = animatable.target;
	var name = tween.name;
	switch (tween.type){
	case 'css':target.style[name] = progress;
	break;
	case 'attribute':target.setAttribute(name,progress);
	break;
	case 'object':target[name] = progress;
	break;
	case 'transform':if (!transforms) transforms ={
}
;
	if (!transforms[id]) transforms[id] = [];
	transforms[id].push(progress);
	break;
}
}
}
if (transforms){
	if (!transform) transform = (getCSSValue(document.body,transformStr) ? '':'-webkit-') + transformStr;
	for (var t in transforms){
	anim.animatables[t].target.style[transform] = transforms[t].join(' ');
}
}
}
// Animation var createAnimation = function(params){
	var anim ={
}
;
	anim.animatables = getAnimatables(params.targets);
	anim.settings = mergeObjects(params,defaultSettings);
	anim.properties = getProperties(params,anim.settings);
	anim.tweens = getTweens(anim.animatables,anim.properties);
	anim.duration = getTweensDuration(anim.tweens) || params.duration;
	anim.delay = getTweensDelay(anim.tweens) || params.delay;
	anim.currentTime = 0;
	anim.progress = 0;
	anim.ended = false;
	return anim;
}
// Public var animations = [];
	var raf = 0;
	var engine = (function(){
	var play = function(){
	raf = requestAnimationFrame(step);
}
;
	var step = function(t){
	if (animations.length){
	for (var i = 0;
	i < animations.length;
	i++) animations[i].tick(t);
	play();
}
else{
	cancelAnimationFrame(raf);
	raf = 0;
}
}
return play;
}
)();
	var animation = function(params){
	var anim = createAnimation(params);
	var time ={
}
;
	anim.tick = function(now){
	anim.ended = false;
	if (!time.start) time.start = now;
	time.current = Math.min(Math.max(time.last + now - time.start,0),anim.duration);
	setAnimationProgress(anim,time.current);
	var s = anim.settings;
	if (time.current >= anim.delay){
	if (s.begin) s.begin(anim);
	s.begin = undefined;
	if (s.update) s.update(anim);
}
if (time.current >= anim.duration){
	if (s.loop){
	time.start = now;
	if (s.direction === 'alternate') reverseTweens(anim,true);
	if (is.num(s.loop)) s.loop--;
}
else{
	anim.ended = true;
	anim.pause();
	if (s.complete) s.complete(anim);
}
time.last = 0;
}
}
anim.seek = function(progress){
	setAnimationProgress(anim,(progress / 100) * anim.duration);
}
anim.pause = function(){
	removeWillChange(anim);
	var i = animations.indexOf(anim);
	if (i > -1) animations.splice(i,1);
}
anim.play = function(params){
	anim.pause();
	if (params) anim = mergeObjects(createAnimation(mergeObjects(params,anim.settings)),anim);
	time.start = 0;
	time.last = anim.ended ? 0:anim.currentTime;
	var s = anim.settings;
	if (s.direction === 'reverse') reverseTweens(anim);
	if (s.direction === 'alternate' && !s.loop) s.loop = 1;
	setWillChange(anim);
	animations.push(anim);
	if (!raf) engine();
}
anim.restart = function(){
	if (anim.reversed) reverseTweens(anim);
	anim.pause();
	anim.seek(0);
	anim.play();
}
if (anim.settings.autoplay) anim.play();
	return anim;
}
// Remove one or multiple targets from all active animations. var remove = function(elements){
	var targets = flattenArray(is.arr(elements) ? elements.map(toArray):toArray(elements));
	for (var i = animations.length-1;
	i >= 0;
	i--){
	var animation = animations[i];
	var tweens = animation.tweens;
	for (var t = tweens.length-1;
	t >= 0;
	t--){
	var animatables = tweens[t].animatables;
	for (var a = animatables.length-1;
	a >= 0;
	a--){
	if (arrayContains(targets,animatables[a].target)){
	animatables.splice(a,1);
	if (!animatables.length) tweens.splice(t,1);
	if (!tweens.length) animation.pause();
}
}
}
}
}
animation.version = version;
	animation.speed = 1;
	animation.list = animations;
	animation.remove = remove;
	animation.easings = easings;
	animation.getValue = getInitialTargetValue;
	animation.path = getPathProps;
	animation.random = random;
	return animation;
}
));
	

CSS代码(styles.css):

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{border:0;font-size:100%;font:inherit;vertical-align:baseline;margin:0;padding:0}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}
body{line-height:1}
ol,ul{list-style:none}
blockquote,q{quotes:none}
blockquote:before,blockquote:after,q:before,q:after{content:none}
table{border-collapse:collapse;border-spacing:0}
@font-face{font-family:'Circular';src:url('../fonts/Circular-Book.woff') format('woff');font-weight:normal;}
@font-face{font-family:'Circular';src:url('../fonts/Circular-Bold.woff') format('woff');font-weight:bold;}
*{margin:0;padding:0;box-sizing:border-box;}
html,body{background:#18212D;}
body{position:absolute;display:flex;align-items:center;justify-content:center;width:100%;height:100%;flex-wrap:wrap;color:white;font-family:'Circular',sans-serif;-webkit-font-smoothing:antialiased;}
section{min-width:18rem;}
div{width:2rem;height:2rem;}
.red{background-color:#FF324A;}
.green{background-color:#31FFA6;}
.blue{background-color:#206EFF;}
.yellow{background-color:#FFFF99;}
附件:下载该文件资源,减少时间成本(增值服务)
留言
该资源可下载
File Source
.rar
11.33 KB
Html JS 其它特效3
最新结算
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
HTML5实现CSS滤镜图片切换特效代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jQuery头像裁剪插件cropbox js代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery头像裁剪插件cropbox js代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery+css3实现信封效果
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
我们力求给您提供有用的文章,再此基础上,会附加营收资源,不做任何广告,让平台可以更好发展 若您发现您的权利被侵害,或使用了您的版权,请发邮件联系 sunlifel@foxmail.com ggbig觉得 : 不提供源码的文章不是好文章
合作伙伴
联系我们
  • QQ:21499807
  • 邮箱:sunlifel@foxmail.com
  • QQ扫一扫加QQ
    QQ扫一扫
Copyright 2023-2024 ggbig.com·皖ICP备2023004211号-1
打赏文章