溫馨提示×

js怎么實現文本顯示的公告欄

js
小億
193
2023-08-01 10:43:48
欄目: 編程語言

在JavaScript中,可以使用DOM操作來實現文本顯示的公告欄。下面是一個簡單的示例:

<!DOCTYPE html>
<html>
<head>
<title>公告欄</title>
<style>
.notice {
width: 400px;
height: 200px;
overflow: hidden;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class="notice">
<marquee id="noticeText" behavior="scroll" direction="left"></marquee>
</div>
<script>
var noticeText = document.getElementById('noticeText');
var notices = ['公告1', '公告2', '公告3']; // 公告內容數組
function showNotice() {
var index = 0;
setInterval(function() {
noticeText.textContent = notices[index];
index = (index + 1) % notices.length;
}, 3000); // 每隔3秒切換一條公告
}
showNotice();
</script>
</body>
</html>

在上面的示例中,使用marquee標簽來實現公告欄的滾動效果。JavaScript部分定義了一個showNotice函數,用于循環顯示公告內容數組中的元素。使用setInterval定時器每隔3秒切換一條公告內容,直到所有公告都顯示完畢。

注意,marquee標簽在HTML5中已被廢棄,不推薦使用??梢允褂肅SS的animation屬性來代替實現滾動效果。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女