html星空特效代码css(HTML代码和CSS代码有什么区别)

本文目录
HTML代码和CSS代码有什么区别
HTML代码和CSS代码有什么区别:
第一种方法:通用方法
1、打开浏览器右键查看源码代码
2、打开一个网页,在该网页中点击左键,如图
3、点击查看源代码,如图,看到了吗,这一大堆字母就是你这个网页的源代码了。
4、上面那个太复杂了,点击工具,点击开发者工具,如图,
5、这样就可以见到更简截的源代码了,如图。
第二种方法:在IE浏览器中查看
1、
2、用IE浏览器打开一个页面,如图。点击网页,
3、点击查看源文件,如图
这样就看到了这个网页的源代码了,如图
4、这个太复杂了看不懂,同样可以看到简截的,点击工具,如图
点击开者工具,如图
5、在这里就看到了HTML代码和CSS代码,如图
星空代码和旋转代码区别在于哪里
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 Matwww.scxhdzs.com#or(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 = 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.draw();
};
window.requestAnimationFrame(animation);
}
animation();
《/script》
《/body》
《/html》
如何用CSS和D3实现宇宙飞船的动态效果
这篇文章给大家介绍的内容是关于如何用CSS和D3实现宇宙飞船的动态效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
效果预览
代码解读定义 dom,spacecraft 表示飞船,容器中包含 1 个表示尾冀的元素 fins:
《div class="spacecraft"》
《div class="fins"》《/div》
《/div》居中显示:
body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(black, midnightblue);
}画出飞船的船舱:
.spacecraft {
width: 7em;
height: 11em;
font-size: 16px;
background:
linear-gradient(whitesmoke, darkgray);
border-radius: 50% / 70% 70% 5% 5%;
}用伪元素画出飞船尾部的火焰:
.spacecraft::before {
content: ’’;
position: absolute;
width: 6em;
height: 2em;
background-color: #444;
border-radius: 20%;
top: 10em;
left: 0.5em;
z-index: -1;
}
.spacecraft::after {
content: ’’;
position: absolute;
box-sizing: border-box;
width: 4em;
height: 4em;
background: gold;
top: 10em;
left: 1.5em;
border-radius: 80% 0 50% 45% / 50% 0 80% 45%;
transform: rotate(135deg);
border: 0.5em solid orange;
z-index: -2;
}画出飞船两侧的尾冀:
.fins::before,
.fins::after {
content: ’’;
position: absolute;
width: 2em;
height: 6em;
background: linear-gradient(tomato, darkred);
top: 7em;
}
.fins::before {
left: -2em;
border-radius: 3em 0 50% 100%;
}
.fins::after {
right: -2em;
border-radius: 0 3em 100% 50%;
}用径向渐变画出飞船的舷窗:
.spacecraft {
background:
radial-gradient(
circle at 3.5em 5em,
transparent 1.5em,
lightslategray 1.5em, lightslategray 2em,
transparent 2em
),
radial-gradient(
circle at 3.3em 5.2em,
deepskyblue 1.4em,
transparent 1.6em
),
radial-gradient(
circle at 3.5em 5em,
white 1.5em,
transparent 1.5em
),
linear-gradient(whitesmoke, darkgray);
}增加飞船火焰喷射的动画效果:
.spacecraft::after {
animation: flame-spout 0.3s infinite;
}
@keyframes flame-spout {
0%, 100% {
filter: opacity(0.1);
}
50% {
filter: opacity(1);
}
}接下来画星空。
在 dom 中增加 stars 容器,其中包含表示星星的 4 个子元素:
《div class="stars"》
《span》《/span》
《span》《/span》
《span》《/span》
《span》《/span》
《/div》
《div class="rocket"》
《div class="fins"》《/div》
《/div》定义星星的样式:
.stars span {
position: absolute;
width: 2px;
height: 8px;
border-radius: 50%;
background-color: white;
top: calc(50% - 7em);
}用变量使星星分布在水平方向的不同位置:
.stars span {
left: calc(var(--left) * 1vw);
}
.stars span:nth-child(1) {
--left: 20;
}
.stars span:nth-child(2) {
--left: 40;
}
.stars span:nth-child(3) {
--left: 60;
}
.stars span:nth-child(4) {
--left: 80;
}用变量设置星星的尺寸和不透明度,使每颗星星看起来稍有差异:
.stars span {
width: calc(var(--size) * 1px);
height: calc(var(--size) * 4px);
filter: opacity(var(--opacity));
}
.stars span:nth-child(1) {
--size: 0.8;
--opacity: 0.5;
}
.stars span:nth-child(2) {
--size: 1.25;
--opacity: 0.6;
}
.stars span:nth-child(3) {
--size: 1.5;
--opacity: 0.7;
}
.stars span:nth-child(4) {
--size: 2;
--opacity: 0.8;
}定义星星从太空中飘过的动画效果:
.stars span {
top: -5vh;
animation: star-move linear infinite;
}
@keyframes star-move {
to {
top: 100vh;
}
}用变量设置动画的时长和延时时间:
.stars span {
animation-duration: calc(var(--duration) * 1s);
animation-delay: calc(var(--delay) * 1s);
}
.stars span:nth-child(1) {
--duration: 1;
--delay: -0.05;
}
.stars span:nth-child(2) {
--duration: 1.5;
--delay: -0.1;
}
.stars span:nth-child(3) {
--duration: 2;
--delay: -0.15;
}
.stars span:nth-child(4) {
--duration: 2.5;
--delay: -0.2;
}隐藏屏幕外的内容:
body {
overflow: hidden;
}接下来用 d3 批量处理表示星星的 dom 元素和 css 变量。
引入 d3 库:
***隐藏网址***
const COUNT_OF_STARS = 4;
d3.select(’.stars’)
.selectAll(’span’)
.data(d3.range(COUNT_OF_STARS))
.enter()
.append(’span’);用 d3 为 css 变量 --left, --size, --opacity 赋值,--left 的取值范围是 1 到 100,--size 的取值范围是 1 到 2.5,’--opacity’ 的取值范围是 0.5 到 0.8:
d3.select(’.stars’)
.selectAll(’span’)
.data(d3.range(COUNT_OF_STARS))
.enter()
.append(’span’)
.style(’--left’, () =》 Math.ceil(Math.random() * 100))
.style(’--size’, () =》 Math.random() * 1.5 + 1)
.style(’--opacity’, () =》 Math.random() * 0.3 + 0.5);用 d3 为 css 变量 --duration 和 --delay 赋值,--duration 的取值范围是 1 到 3,--delay 的取值是依次减少 0.05:
d3.select(’.stars’)
.selectAll(’span’)
.data(d3.range(COUNT_OF_STARS))
.enter()
.append(’span’)
.style(’--left’, () =》 Math.ceil(Math.random() * 100))
.style(’--size’, () =》 Math.random() * 1.5 + 1)
.style(’--opacity’, () =》 Math.random() * 0.3 + 0.5)
.style(’--duration’, () =》 Math.random() * 2 + 1)
.style(’--delay’, (d) =》 d * -0.05);_除掉 html 文件中相关的 dom 声明和 css 文件中的变量声明。
最后,把星星的数量增加到 30 颗:
const COUNT_OF_STARS = 30;大功告成!
相关文章推荐:
如何使用纯CSS实现一颗土星的效果
如何使用CSS和D3实现无尽六边形空间的效果

更多文章:
进制转换的8421法(十进制如何转换成8421码~~请说方法)
2026年5月11日 00:45
线程和进程的区别是什么 面试(linux进程和线程的区别 面试)
2026年1月20日 03:15
rabbitmq最新版本(rabbitmq3.8.2安装步骤及集群配置)
2025年11月21日 11:00
易语言源码打开停止工作(同样一个易语言源码,为什么在我的电脑上运行就卡死,在别人的电脑上运行是正常的)
2026年5月15日 01:30
powershell快捷键打开(windows10的控制面板在哪里找)
2026年8月7日 07:15
oracle数据库完整性(oracle添加记录的时候提示违反完整性约束,未找到父项关键字怎么解决)
2025年10月15日 22:30
java找不到工作想去送快递了(昌平北大青鸟分享学Java找不到工作的原因)
2026年8月6日 10:45
后台管理系统模板如何使用(开源网店iWeb Shop的模板管理后台怎么操作有什么值得注意的地方)
2025年10月30日 18:15
高级程序设计语言的主要功能(高级语言的控制结构主要包含什么)
2025年12月8日 19:00
mapreduce的优势(hadoop与传统的关系型数据库(如oracle)相比,有什么优势及劣势)
2026年2月12日 15:00
java交互界面怎么做(北大青鸟java培训:常见交互设计表现形式)
2026年3月23日 04:00
pull together(pull together是什么意思)
2026年1月6日 13:30
substantial变形单词(哪些英语单词可以表达 “重要的” 以及它们的区别)
2026年1月13日 17:00
floor和ceil函数(Math之ceil、floor、round区别)
2026年1月22日 22:30
汉化版eclipse(在汉化版eclipse中编写java程序,无法解析start是什么意思)
2025年8月6日 22:30
interesting怎么读英语(“interesting”怎么读)
2026年5月11日 15:45











