Skip to content

Commit

Permalink
feat: 首页公告标题增加打字机效果 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
wan92hen authored Dec 7, 2023
1 parent 490ebb6 commit 04f4acb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
@import 'post';
@import 'pagination';
@import 'footer';
@import 'typed-text';
14 changes: 14 additions & 0 deletions src/styles/typed-text.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* 光标样式 */
.typed-text::after {
content: '|';
display: inline-block;
opacity: 0;
animation: blink 0.7s infinite alternate; /* 闪烁动画 */
}

/* 闪烁动画 */
@keyframes blink {
to {
opacity: 1;
}
}
24 changes: 22 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<th:block th:fragment="content">
<div class="content">
<div class="index-content framed">
<h1 th:text="${theme.config.basic.index_notice_title}"></h1>
<p th:utext="${theme.config.basic.index_notice_content}"></p>
<h1 class="typed-text" th:text="${theme.config.basic.index_notice_title}"></h1>
<div th:utext="${theme.config.basic.index_notice_content}"></div>
</div>
<div class="posts">
<div class="post on-list" th:each="post : ${posts.items}">
Expand Down Expand Up @@ -58,5 +58,25 @@ <h1 class="post-title">
</div>
</div>
</div>
<script>
// 获取容器
const typedTextContainer = document.querySelector('.typed-text');

// 打字机效果的实现
function typeWriter(container) {
text = container.innerText;
container.innerText = '';
let i = 0;
function type() {
if (i < text.length) {
container.innerHTML += text.charAt(i);
i++;
setTimeout(type, Math.random() * 200 + 50);
}
}
type();
}
typeWriter(typedTextContainer);
</script>
</th:block>
</html>

0 comments on commit 04f4acb

Please sign in to comment.