星空特效的HTML代碼示例

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

以下是一個星空特效的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代碼示例” 的相關文章

meter元素顏色,可以使用CSS樣式來設置顏色

meter元素顏色,可以使用CSS樣式來設置顏色

meter元素可以用於表示已知範圍內的度量值,可以使用CSS樣式來設置顏色。具體來說,可以使用 <code>::-webkit-meter-optimum-value, ::-moz-meter-bar, ::-webkit-meter-bar</code> 偽元素來設置顏色。下面的例子中,我們將 <code>meter</code>。上述代碼中,當 <code>meter</code> 元素的值在80時,最優值(optimum)的顏色為綠色;當值落在0~80之間時,表格的顏色為灰色。可以按照自己的需求設置這些顏色值。…

css高亮效果,CSS 創建不同高亮效果具體方法及例子

css高亮效果,CSS 創建不同高亮效果具體方法及例子

CSS 可以為  元素創建各種不同的高亮效果,具體方法如下:1. 使用默認的高亮樣式。可以通過樣式修改  元素的背景色和文本顏色,實現自定義高亮效果,例如:```html這是一段需要高亮的文本,這裏面的文本被自定義高亮了,這裏又是正常的文本。.highlight {  position: relative;}.highlight::before {  content: "";  position: absolute;  top: 0;  left: 0;  width: 100%;  height: 100%;  background-color: lightgreen;  opacity: 0.5;  z-index: -1;}```上述代碼會將  元素的文本區域覆蓋上高亮色,實現了高亮效果。…

視頻嵌入代碼,簡單的 video 嵌入代碼例子

視頻嵌入代碼,簡單的 video 嵌入代碼例子

視頻嵌入代碼,簡單的 video 嵌入代碼例子:- height:視頻高度。- controls:為 true 時,添加視頻控制條。- source:指定視頻文件路徑和類型,可支持多種類型。- Your browser does not support the video tag:如果用戶的瀏覽器不支持 HTML5  標記,則會顯示此消息。值得註意的是,這種視頻嵌入方式可能會因為用戶瀏覽器兼容性問題而無法播放,因此可能需要添加備用方案,如 Flash 等。同時,需要根據實際情況調整視頻的寬高比例、大小和文件大小等參數,以便更好地適配不同的設備和網絡環境。…

HTML 和 CSS 實現網頁導航欄和下拉菜單

HTML 和 CSS 實現網頁導航欄和下拉菜單

以下是一組基於 HTML 和 CSS 實現網頁導航欄和下拉菜單的完整代碼:HTML 代碼:CSS 代碼。該代碼實現了一個簡單的網頁導航欄和下拉菜單,其中 `nav` 表示導航欄,`ul` 表示菜單,`li` 表示菜單項,`a` 表示鏈接。通過設置 CSS 樣式,將菜單項設置為了懸停時出現下拉菜單,同時用絕對定位實現了下拉菜單的顯示。在實際應用中,可能需要更多的樣式和JavaScript交互來完善導航欄和下拉菜單的功能。…

JS跳轉頁面代碼及例子

JS跳轉頁面代碼及例子

JS跳轉頁面是一種很常見的前端交互技術,下面是幾種跳轉頁面的方式:1. 直接修改 `window.location.href` 屬性,2. 使用 `window.location.replace` 方法,此方法會替換當前頁面歷史記錄,不會在瀏覽器歷史記錄中留下痕跡。3. 使用 `window.open` 方法在一個新的瀏覽器窗口或標簽頁中打開一個頁面,4. 如果你需要在某個時間間隔後自動跳轉到目標頁面,可以使用 `setTimeout` 函數。…

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

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

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