添加深色模式切换功能,优化样式以支持深色主题

This commit is contained in:
2025-03-06 22:33:49 +08:00
parent 411e3436f1
commit ad6241012a
5 changed files with 95 additions and 6 deletions

View File

@ -10,14 +10,29 @@
<el-menu-item @click="openLink('/')">首页</el-menu-item> <el-menu-item @click="openLink('/')">首页</el-menu-item>
<el-menu-item @click="goToBlog">博客</el-menu-item> <el-menu-item @click="goToBlog">博客</el-menu-item>
<el-menu-item @click="openLink('/links')">友链</el-menu-item> <el-menu-item @click="openLink('/links')">友链</el-menu-item>
<el-menu-item @click="toggleDarkMode">
<div class="flip-container">
<div :class="['flipper', isDarkMode ? 'flip' : '']">
<el-icon class="front">
<Sunny />
</el-icon>
<el-icon class="back">
<Moon />
</el-icon>
</div>
</div>
</el-menu-item>
</div> </div>
</el-menu> </el-menu>
</template> </template>
<script setup> <script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { Sunny, Moon } from '@element-plus/icons-vue';
const router = useRouter(); const router = useRouter();
const isDarkMode = ref(false);
// 打开外部链接 // 打开外部链接
const goToBlog = () => { const goToBlog = () => {
@ -29,6 +44,23 @@ const openLink = (path) => {
console.log("Navigating to:", path); // 调试信息 console.log("Navigating to:", path); // 调试信息
router.push(path); router.push(path);
}; };
const toggleDarkMode = () => {
isDarkMode.value = !isDarkMode.value;
document.documentElement.classList.toggle('dark', isDarkMode.value);
localStorage.setItem('darkMode', isDarkMode.value);
};
// 初始化时读取 localStorage 中的模式选择
const initializeDarkMode = () => {
const savedMode = localStorage.getItem('darkMode');
if (savedMode !== null) {
isDarkMode.value = savedMode === 'true';
document.documentElement.classList.toggle('dark', isDarkMode.value);
}
};
initializeDarkMode();
</script> </script>
<style scoped> <style scoped>
@ -56,4 +88,41 @@ const openLink = (path) => {
.website-name { .website-name {
font-size: 1.5em; font-size: 1.5em;
} }
.el-menu-item {
outline: none !important;
background-color: transparent !important;
}
.flip-container {
perspective: 1000px;
}
.flipper {
position: relative;
width: 24px;
height: 24px;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.flip {
transform: rotateY(180deg);
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.front {
transform: rotateY(0deg);
}
.back {
transform: rotateY(180deg);
}
</style> </style>

View File

@ -7,6 +7,7 @@ import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import './assets/styles/global.css' import './assets/styles/global.css'
import '@fortawesome/fontawesome-free/css/all.css'; import '@fortawesome/fontawesome-free/css/all.css';
import 'github-calendar/dist/github-calendar-responsive.css'; import 'github-calendar/dist/github-calendar-responsive.css';
import 'element-plus/theme-chalk/dark/css-vars.css'
const app = createApp(App) const app = createApp(App)

View File

@ -7,6 +7,9 @@
color: rgba(255, 255, 255, 0.87); color: rgba(255, 255, 255, 0.87);
background-color: #242424; background-color: #242424;
--background-color: #242424;
--text-color: #3d3d3d;
font-synthesis: none; font-synthesis: none;
text-rendering: optimizeLegibility; text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
@ -18,10 +21,16 @@ a {
color: #646cff; color: #646cff;
text-decoration: inherit; text-decoration: inherit;
} }
a:hover { a:hover {
color: #535bf2; color: #535bf2;
} }
.dark {
--background-color: #242424;
--text-color: rgb(228, 228, 228);
}
body { body {
margin: 0; margin: 0;
display: flex; display: flex;
@ -42,7 +51,8 @@ button {
font-size: 1em; font-size: 1em;
font-weight: 500; font-weight: 500;
font-family: inherit; font-family: inherit;
background-color: #1a1a1a; background-color: var(--background-color);
color: var(--text-color);
cursor: pointer; cursor: pointer;
transition: border-color 0.25s; transition: border-color 0.25s;
} }
@ -77,3 +87,7 @@ button:focus-visible {
background-color: #f9f9f9; background-color: #f9f9f9;
} }
} }
p,a,h1,h2,h3,h4,h5,h6 {
color: var(--text-color);
}

View File

@ -226,7 +226,7 @@ onUnmounted(() => {
} }
.social-icons a { .social-icons a {
color: #303133; color: var(--text-color);
margin: 0 10px; margin: 0 10px;
font-size: 24px; font-size: 24px;
transition: color 0.3s ease, transform 0.3s ease; transition: color 0.3s ease, transform 0.3s ease;
@ -279,7 +279,7 @@ onUnmounted(() => {
.blog-title { .blog-title {
font-size: 1.2em; font-size: 1.2em;
font-weight: bold; font-weight: bold;
color: #303133; color: var(--text-color);
text-decoration: none; text-decoration: none;
margin-left: 10px; margin-left: 10px;
} }
@ -292,7 +292,7 @@ onUnmounted(() => {
display: flex; display: flex;
align-items: center; align-items: center;
font-size: 0.9em; font-size: 0.9em;
color: #606266; color: var(--text-color);
} }
.update-time, .update-time,
@ -300,11 +300,12 @@ onUnmounted(() => {
display: flex; display: flex;
align-items: center; align-items: center;
margin-right: 20px; margin-right: 20px;
color: var(--text-color);
} }
.icon { .icon {
margin-right: 5px; margin-right: 5px;
color: #909399; color: var(--text-color);
} }
.tag { .tag {
@ -344,7 +345,7 @@ onUnmounted(() => {
} }
.read-more-icon { .read-more-icon {
color: #409eff; color: var(--text-color);
transition: transform 0.3s ease; transition: transform 0.3s ease;
} }

View File

@ -209,4 +209,8 @@ h1 {
align-items: center; align-items: center;
gap: 10px; gap: 10px;
} }
.link-content {
color: var(--text-color);
}
</style> </style>