来自codepen的CSS和Javascript动画不工作
我从一篇codepen文章中得到了以下代码,我试图使用一个html页面重新创建这些代码。设计很好,但动画似乎不起作用 代码笔链接在这里: 我拥有的代码(从上面复制)以及我使用的codepen编译的CSS如下所示:来自codepen的CSS和Javascript动画不工作,javascript,css,animation,codepen,Javascript,Css,Animation,Codepen,我从一篇codepen文章中得到了以下代码,我试图使用一个html页面重新创建这些代码。设计很好,但动画似乎不起作用 代码笔链接在这里: 我拥有的代码(从上面复制)以及我使用的codepen编译的CSS如下所示: <html> <head> <style> /* Header */ .large-header { position: relative; width: 100%; background: #333; overflow: hidd
<html>
<head>
<style>
/* Header */
.large-header {
position: relative;
width: 100%;
background: #333;
overflow: hidden;
background-size: cover;
background-position: center center;
z-index: 1;
}
#large-header {
background-image: url("https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/img/demo-1-bg.jpg");
}
.main-title {
position: absolute;
margin: 0;
padding: 0;
color: #f9f1e9;
text-align: center;
top: 50%;
left: 50%;
-webkit-transform: translate3d(-50%, -50%, 0);
transform: translate3d(-50%, -50%, 0);
}
.demo-1 .main-title {
text-transform: uppercase;
font-size: 4.2em;
letter-spacing: 0.1em;
}
.main-title .thin {
font-weight: 200;
}
@media only screen and (max-width: 768px) {
.demo-1 .main-title {
font-size: 3em;
}
}
</style>
</head>
<body>
<div id="large-header" class="large-header">
<canvas id="demo-canvas"></canvas>
<h1 class="main-title"><span class="thin">TitleGoes <span class="thin">.here</span></h1>
</div>
<script>
(function() {
var width, height, largeHeader, canvas, ctx, points, target, animateHeader = true;
// Main
initHeader();
initAnimation();
addListeners();
function initHeader() {
width = window.innerWidth;
height = window.innerHeight;
target = {x: width/2, y: height/2};
largeHeader = document.getElementById('large-header');
largeHeader.style.height = height+'px';
canvas = document.getElementById('demo-canvas');
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext('2d');
// create points
points = [];
for(var x = 0; x < width; x = x + width/20) {
for(var y = 0; y < height; y = y + height/20) {
var px = x + Math.random()*width/20;
var py = y + Math.random()*height/20;
var p = {x: px, originX: px, y: py, originY: py };
points.push(p);
}
}
// for each point find the 5 closest points
for(var i = 0; i < points.length; i++) {
var closest = [];
var p1 = points[i];
for(var j = 0; j < points.length; j++) {
var p2 = points[j]
if(!(p1 == p2)) {
var placed = false;
for(var k = 0; k < 5; k++) {
if(!placed) {
if(closest[k] == undefined) {
closest[k] = p2;
placed = true;
}
}
}
for(var k = 0; k < 5; k++) {
if(!placed) {
if(getDistance(p1, p2) < getDistance(p1, closest[k])) {
closest[k] = p2;
placed = true;
}
}
}
}
}
p1.closest = closest;
}
// assign a circle to each point
for(var i in points) {
var c = new Circle(points[i], 2+Math.random()*2, 'rgba(255,255,255,0.3)');
points[i].circle = c;
}
}
// Event handling
function addListeners() {
if(!('ontouchstart' in window)) {
window.addEventListener('mousemove', mouseMove);
}
window.addEventListener('scroll', scrollCheck);
window.addEventListener('resize', resize);
}
function mouseMove(e) {
var posx = posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
target.x = posx;
target.y = posy;
}
function scrollCheck() {
if(document.body.scrollTop > height) animateHeader = false;
else animateHeader = true;
}
function resize() {
width = window.innerWidth;
height = window.innerHeight;
largeHeader.style.height = height+'px';
canvas.width = width;
canvas.height = height;
}
// animation
function initAnimation() {
animate();
for(var i in points) {
shiftPoint(points[i]);
}
}
function animate() {
if(animateHeader) {
ctx.clearRect(0,0,width,height);
for(var i in points) {
// detect points in range
if(Math.abs(getDistance(target, points[i])) < 4000) {
points[i].active = 0.3;
points[i].circle.active = 0.6;
} else if(Math.abs(getDistance(target, points[i])) < 20000) {
points[i].active = 0.1;
points[i].circle.active = 0.3;
} else if(Math.abs(getDistance(target, points[i])) < 40000) {
points[i].active = 0.02;
points[i].circle.active = 0.1;
} else {
points[i].active = 0;
points[i].circle.active = 0;
}
drawLines(points[i]);
points[i].circle.draw();
}
}
requestAnimationFrame(animate);
}
function shiftPoint(p) {
TweenLite.to(p, 1+1*Math.random(), {x:p.originX-50+Math.random()*100,
y: p.originY-50+Math.random()*100, ease:Circ.easeInOut,
onComplete: function() {
shiftPoint(p);
}});
}
// Canvas manipulation
function drawLines(p) {
if(!p.active) return;
for(var i in p.closest) {
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(p.closest[i].x, p.closest[i].y);
ctx.strokeStyle = 'rgba(156,217,249,'+ p.active+')';
ctx.stroke();
}
}
function Circle(pos,rad,color) {
var _this = this;
// constructor
(function() {
_this.pos = pos || null;
_this.radius = rad || null;
_this.color = color || null;
})();
this.draw = function() {
if(!_this.active) return;
ctx.beginPath();
ctx.arc(_this.pos.x, _this.pos.y, _this.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'rgba(156,217,249,'+ _this.active+')';
ctx.fill();
};
}
// Util
function getDistance(p1, p2) {
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
}
})();
</script>
</body>
</html>
/*标题*/
.大标题{
位置:相对位置;
宽度:100%;
背景:#333;
溢出:隐藏;
背景尺寸:封面;
背景位置:中心;
z指数:1;
}
#大割台{
背景图像:url(“https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/img/demo-1-bg.jpg");
}
.主标题{
位置:绝对位置;
保证金:0;
填充:0;
颜色:#f9f1e9;
文本对齐:居中;
最高:50%;
左:50%;
-webkit转换:translate3d(-50%,-50%,0);
转换:translate3d(-50%,-50%,0);
}
.demo-1.主标题{
文本转换:大写;
字体大小:4.2米;
字母间距:0.1米;
}
.主标题.瘦{
字号:200;
}
@仅介质屏幕和(最大宽度:768px){
.demo-1.主标题{
字号:3em;
}
}
TitleGoes,给你
(功能(){
变量宽度、高度、大标题、画布、ctx、点、目标、动画标题=真;
//主要
initHeader();
初始化动画();
addListeners();
函数initHeader(){
宽度=window.innerWidth;
高度=窗内高度;
目标={x:width/2,y:height/2};
largeHeader=document.getElementById('large-header');
largeHeader.style.height=高度+'px';
canvas=document.getElementById('demo-canvas');
画布宽度=宽度;
canvas.height=高度;
ctx=canvas.getContext('2d');
//创建点
点数=[];
对于(变量x=0;xheight)animateHeader=false;
else animateHeader=true;
}
函数resize(){
宽度=window.innerWidth;
高度=窗内高度;
largeHeader.style.height=高度+'px';
画布宽度=宽度;
canvas.height=高度;
}
//动画
函数initAnimation(){
制作动画();
用于(以点为单位的var i){
移位点(点[i]);
}
}
函数animate(){
如果(动画领导者){
ctx.clearRect(0,0,宽度,高度);
用于(以点为单位的var i){
//检测范围内的点
if(Math.abs(getDistance(目标,点[i]))<4000){
点[i]。激活=0.3;
点[i].circle.active=0.6;
}else if(Math.abs(getDistance(target,points[i]))<20000){
点[i]。活动=0.1;
点[i].circle.active=0.3;
}else if(Math.abs(getDistance(target,points[i]))<40000){
点[i]。激活=0.02;
点[i].circle.active=0.1;
}否则{
点[i]。活动=0;
点[i].circle.active=0;
}
抽绳(点[i]);
点[i]。圆。画();
}
}
请求动画帧(动画);
}
函数移位点(p){
TweenLite.to(p,1+1*Math.random(),{x:p.originX-50+Math.random()*100,
y:p.originY-50+Math.random()*100,ease:Circ.easeInOut,
onComplete:function(){
移位点(p);
}});
}
//画布操纵
功能抽绳(p){
如果(!p.active)retu
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:200,400,800" />
<link rel="stylesheet" href="https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/css/demo.css" />
<script src="https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/js/EasePack.min.js"></script>
<script src="https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/js/rAF.js"></script>
<script src="https://www.marcoguglie.it/Codepen/AnimatedHeaderBg/demo-1/js/TweenLite.min.js"></script>