星空特效的HTML代碼示例

2023-05-11雲端運算與程式碼1660

以下是一個星空特效的HTML代碼示例:


星空特效的HTML代碼示例

```html

<!DOCTYPE html>
<html>
<head>
<title>星空特效示例</title>
<style>
body {
background-color: #000;
}
.stars {
  height: 100%;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  }
.twinkle {
position: absolute;
  width: 2px;
  height: 2px;
  background: #fff;
  box-shadow: 1px 1px 2px #fff;
  animation: twinkle 1s ease-in-out infinite;
}
@keyframes twinkle {
0% { opacity: 1; }
  50% { opacity: 0; }
  100% { opacity: 1; }
}
</style>
</head>
<body>
<div class="stars">
<script>
for (var i = 0; i < 100; i++) {
var star = document.createElement('div');
star.className = 'twinkle';
star.style.left = Math.floor(Math.random() * 100) + '%';
  star.style.top = Math.floor(Math.random() * 100) + '%';
  document.body.appendChild(star);
}
</script>
</div>
</body>
</html>

```


這個代碼會在頁面背景中生成100個閃爍的星星,使得整個頁面看起來像是夜空中的星空。可以在瀏覽器中運行查看效果。


以下是另外一個星空特效的HTML代碼示例:


Example:


```html

<!DOCTYPE html>
<html>
<head>
<title>星空特效示例</title>
<style>
body {
background-color: #000;
}
.stars {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
.star {
position: absolute;
background-color: white;
width: 2px;
height: 2px;
border-radius: 50%;
-webkit-animation: twinkle 3s infinite;
animation: twinkle 3s infinite;
}
.star:nth-child(1) {
animation-delay: 1s;
}
.star:nth-child(2) {
animation-delay: 2s;
}
.star:nth-child(3) {
animation-delay: 3s;
}
.star:nth-child(4) {
animation-delay: 4s;
}
.star:nth-child(5) {
animation-delay: 5s;
}
@keyframes twinkle {
0% {
transform: scale(0.5);
opacity: 1;
}
50% {
transform: scale(1.2);
opacity: 0.2;
}
100% {
transform: scale(0.5);
opacity: 1;
}
}
</style>
</head>
<body>
<div class="stars">
<script>
for (var i = 0; i < 100; i++) {
var star = document.createElement('div');
star.classList.add("star");
star.style.left = Math.floor(Math.random()*100) + "%";
star.style.top = Math.floor(Math.random()*100) + "%";
document.body.appendChild(star);
}
</script>
</div>
</body>
</html>

```


這些代碼可以在瀏覽器中運行並產生相應的星空特效。

分享給朋友:

“星空特效的HTML代碼示例” 的相關文章

JS跳轉頁面代碼及例子

JS跳轉頁面代碼及例子

JS跳轉頁面是一種很常見的前端交互技術,下面是幾種跳轉頁面的方式:1. 直接修改 `window.location.href` 屬性,2. 使用 `window.location.replace` 方法,此方法會替換當前頁面歷史記錄,不會在瀏覽器歷史記錄中留下痕跡。3. 使用 `window.ope…

javascript怎麼改變字體顏色文本顏色代碼

javascript怎麼改變字體顏色文本顏色代碼

這裏是一個簡單的 JavaScript改變文體顏色代碼示例,它會在頁面上創建一個按鈕,點擊該按鈕會使文本顏色發生變化。這段代碼首先在頁面中創建了一個按鈕,然後獲取該按鈕和一個段落元素的引用,接著為按鈕添加了一個事件監聽器,當按鈕被點擊時,段落文本顏色將變成紅色。…

一個簡單的html結婚特效的代碼

一個簡單的html結婚特效的代碼

以下是一個簡單的html結婚特效的代碼:1. 使用html和css定義了一個結婚的基本樣式;2. 分別定義新娘和新郎,並使用background-image設置其背景圖;3. 定義一個心形圖案,並使用animation讓其跳動。…

用html和CSS3制作酷炫的導航欄代碼及例子

用html和CSS3制作酷炫的導航欄代碼及例子

使用HTML5和CSS3的新特性可以制作出很多酷炫的導航欄效果,例如下拉菜單、響應式導航欄、帶有動態效果的導航欄等等。下面以下拉菜單為例,具體步驟如下:1. 創建 HTML 結構;2. 設置基本樣式;3. 添加動態效果。這樣就可以制作出下拉菜單效果,當滑鼠懸停在菜單項上時,菜單項下面的下拉菜單顯示出…

html制作網頁教程技能及代碼例子

html制作網頁教程技能及代碼例子

而HTML作為網頁的標準語言,學習HTML制作網頁的基本技能則是入門網頁制作的必修課程。在這篇文章中,我們將為大家講解一份詳細的HTML制作網頁教程,幫助初學者快速掌握網頁制作技巧,實現自己的網站夢想。以下是一個基礎的HTML網頁制作教程,步驟如下:1. 創建網頁文件 首先,打開一個文本編輯器,如W…