html星空表白特效代码(html星空表白特效代码大全)
html中可以星空的背景吗
可以的
例如下面这段代码
body{
background:?url(图片)?no-repeat?0?0?transparent;
background-size:?200%?auto;//防止图片不够大
}
星空代码和旋转代码区别在于哪里
html5特效代码旋转的星空,首先要设置背景为暗一点的背景色或者图片,例如设置成黑色的背景颜色,然后把代码复制到html文件里,设置下下方JS里的一些设置,如星星的数量、星星移动的范围、星星的大小等,保存后用支持html5的浏览器打开就能看到旋转的星空特效。
温馨提示:如果使用复制按钮代码出现错误,建议使用ctrl+c来复制代码。
!doctype html
html
head
meta charset="utf-8"
title旋转的星空/title
style type="text/css"
body{background: black;padding: 0;margin: 0; overflow:hidden}
.header{margin: 0 auto;width: 100%;height: 100%;background-color: #000;position: relative;}
/style
/head
body
div class="header"canvas id="canvas"/canvas/div
script
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
w = canvas.width = window.innerWidth,
h = canvas.height = window.innerHeight,
hue = 217,
stars = [],
count = 0,
maxStars = 3000;//星星数量
var canvas2 = document.createElement('canvas'),
ctx2 = canvas2.getContext('2d');
canvas2.width = 100;
canvas2.height = 100;
var half = canvas2.width / 2,
gradient2 = ctx2.createRadialGradient(half, half, 0, half, half, half);
gradient2.addColorStop(0.025, '#CCC');
gradient2.addColorStop(0.1, 'hsl(' + hue + ', 61%, 33%)');
gradient2.addColorStop(0.25, 'hsl(' + hue + ', 64%, 6%)');
gradient2.addColorStop(1, 'transparent');
ctx2.fillStyle = gradient2;
ctx2.beginPath();
ctx2.arc(half, half, half, 0, Math.PI * 2);
ctx2.fill();
// End cache
function random(min, max) {
if (arguments.length 2) {
max = min;
min = 0;
}
if (min max) {
var hold = max;
max = min;
min = hold;
}
return Mat(Math.random() * (max - min + 1)) + min;
}
function maxOrbit(x, y) {
var max = Math.max(x, y),
diameter = Math.round(Math.sqrt(max * max + max * max));
return diameter / 2;
//星星移动范围,值越大范围越小,
}
var Star = function() {
this.orbitRadius = random(maxOrbit(w, h));
this.radius = random(60, this.orbitRadius) / 8;
//星星大小
this.orbitX = w / 2;
this.orbitY = h / 2;
this.timePassed = random(0, maxStars);
this.speed = random(this.orbitRadius) / 50000;
//星星移动速度
this.alpha = random(2, 10) / 10;
count++;
stars[count] = this;
}
Star.prototype.draw = function() {
var x = Math.sin(this.timePassed) * this.orbitRadius + this.orbitX,
y = Math.cos(this.timePassed) * this.orbitRadius + this.orbitY,
twinkle = random(10);
if (twinkle === 1 this.alpha 0) {
this.alpha -= 0.05;
} else if (twinkle === 2 this.alpha 1) {
this.alpha += 0.05;
}
ctx.globalAlpha = this.alpha;
ctx.drawImage(canvas2, x - this.radius / 2, y - this.radius / 2, this.radius, this.radius);
this.timePassed += this.speed;
}
for (var i = 0; i maxStars; i++) {
new Star();
}
function animation() {
ctx.globalCompositeOperation = 'source-over';
ctx.globalAlpha = 0.5; //尾巴
ctx.fillStyle = 'hsla(' + hue + ', 64%, 6%, 2)';
ctx.fillRect(0, 0, w, h)
ctx.globalCompositeOperation = 'lighter';
for (var i = 1, l = stars.length; i l; i++) {
stars[i].draw();
};
window.requestAnimationFrame(animation);
}
animation();
/script
/body
/html
求网页的星空背景代码
要完成此效果需要两个步骤
第一步:把如下代码加入到body区域中
script language="JavaScript"
!--
SmallStars = 30;
LargeStars = 10;
SmallYpos = new Array();
SmallXpos = new Array();
LargeYpos = new Array();
LargeXpos = new Array();
Smallspeed= new Array();
Largespeed= new Array();
ns=(document.layers)?1:0;
if (ns){
for (i = 0; i SmallStars; i++)
{document.write("LAYER NAME= sn"+i+" LEFT=0 TOP=0 BGCOLOR= #FFFFF0 CLIP= 0,0,1,1 /LAYER")}
for (i = 0; i LargeStars; i++)
{document.write("LAYER NAME= ln"+i+" LEFT=0 TOP=0 BGCOLOR= #FFFFFF CLIP= 0,0,2,2 /LAYER")}
}
else{
document.write( div style="position:absolute;top:0px;left:0px" );
document.write( div style="position:relative" );
for (i = 0; i SmallStars; i++)
{document.write( div id="si" style="position:absolute;top:0;left:0;width:1px;height:1px;background:#fffff0;font-size:1px"/div )}
document.write( /div );
document.write( /div );
document.write( div style="position:absolute;top:0px;left:0px" );
document.write( div style="position:relative" );
for (i = 0; i LargeStars; i++)
{document.write( div id="li" style="position:absolute;top:0;left:0;width:2px;height:2px;background:#ffffff;font-size:2px"/div )}
document.write( /div );
document.write( /div );
}
WinHeight=(document.layers)?window.innerHeight:window.document.body.clientHeight;
WinWidth=(document.layers)?window.innerWidth:window.document.body.clientWidth;
//Inital placement!
for (i=0; i SmallStars; i++)
{
SmallYpos[i] = Math.round(Math.random()*WinHeight);
SmallXpos[i] = Math.round(Math.random()*WinWidth);
Smallspeed[i]= Math.random()*5+1;
}
for (i=0; i LargeStars; i++)
{
LargeYpos[i] = Math.round(Math.random()*WinHeight);
LargeXpos[i] = Math.round(Math.random()*WinWidth);
Largespeed[i]= Math.random()*10+5;
}
function fly(){
var WinHeight=(document.layers)?window.innerHeight:window.document.body.clientHeight;
var WinWidth=(document.layers)?window.innerWidth:window.document.body.clientWidth;
var hscrll=(document.layers)?window.pageYOffset:document.body.scrollTop;
var wscrll=(document.layers)?window.pageXOffset:document.body.scrollLeft;
for (i=0; i LargeStars; i++)
{
LargeXpos[i]-=Largespeed[i];
if (LargeXpos[i] -10)
{
LargeXpos[i]=WinWidth;
LargeYpos[i]=Math.round(Math.random()*WinHeight);
Largespeed[i]=Math.random()*10+5;
}
if (ns){
document.layers[ ln +i].left=LargeXpos[i];
document.layers[ ln +i].top=LargeYpos[i]+hscrll;
}
else{
li[i].style.pixelLeft=LargeXpos[i];
li[i].style.pixelTop=LargeYpos[i]+hscrll;
}
}
for (i=0; i SmallStars; i++)
{
SmallXpos[i]-=Smallspeed[i];
if (SmallXpos[i] -10)
{
SmallXpos[i]=WinWidth;
SmallYpos[i]=Math.round(Math.random()*WinHeight);
Smallspeed[i]=Math.random()*5+1;
}
if (ns){
document.layers[ sn +i].left=SmallXpos[i];
document.layers[ sn +i].top=SmallYpos[i]+hscrll;
}
else{
si[i].style.pixelLeft=SmallXpos[i];
si[i].style.pixelTop=SmallYpos[i]+hscrll;
}
}
setTimeout( fly() ,10);
}
//fly();
//--
/script
第二步:把“onLoad="fly()"”加在body标记里
例如:body onLoad="fly()"
html爱心特效代码怎么添加名字
html爱心特效代码添加名字的步骤:
1、在主代码最后增加一个模块document.getelementbyid。
2、在模块document.getelementbyid中增加一个标签div。
3、在标签div中添加所要添加的名字。
4、在主代码中增加模块document.getelementbyid即可完成添加名字。