这是一个简单的主题管理插件,允许用户在浅色模式、深色模式及自定义主题之间切换,并实时调整主题的 CSS 变量。该插件通过加载 JSON 配置文件来管理不同的主题样式,并提供了便捷的界面让用户动态更改颜色。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

177 lines
5.2 KiB

<!DOCTYPE html>
<html lang="zh" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>主题管理</title>
<style>
/* 基础样式 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: var(--background-color);
color: var(--text-color);
transition: background-color 0.3s, color 0.3s;
}
h1, h2 {
text-align: center;
color: var(--primary-color);
}
h2 {
margin-top: 40px;
}
div {
margin: 20px;
padding: 20px;
background-color: var(--secondary-color);
border-radius: 8px;
box-shadow: 0 4px 10px var(--shadow-color);
}
button {
margin: 10px;
padding: 10px 20px;
border: none;
background-color: var(--primary-color);
color: white;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
button:hover {
background-color: var(--accent-color);
}
label {
display: inline-block;
width: 100px;
margin-right: 10px;
font-weight: bold;
}
input[type="color"] {
width: 50px;
height: 30px;
border: none;
cursor: pointer;
}
.theme-control {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 20px;
}
.theme-control div {
display: flex;
align-items: center;
border: 1px solid var(--border-color);
}
.current-theme {
font-weight: bold;
color: var(--primary-color);
}
</style>
<script type="module">
import useThemeManager from '../dist/themeManager.js';
// 初始化 ThemeManager
const { loadThemesFromJSON, switchTheme, setThemeVariable, onThemeChange } = useThemeManager();
// 加载 JSON 主题(如果有远程 JSON)
loadThemesFromJSON('./themes.json');
// 切换主题函数
function handleSwitchTheme(theme) {
switchTheme(theme);
}
window.handleSwitchTheme = handleSwitchTheme;
window.setThemeVariable = setThemeVariable;
// 修改 CSS 变量
setThemeVariable('--primary-color', '#ff6600');
setThemeVariable('--success-color', '#00c853');
// 监听主题变化
onThemeChange((newTheme) => {
console.log("🎨 主题已切换为:", newTheme);
document.getElementById('themeName').innerText = newTheme;
});
</script>
</head>
<body>
<h1>🎨 主题管理</h1>
<div class="theme-control">
<button onclick="handleSwitchTheme('light')">🌞 浅色模式</button>
<button onclick="handleSwitchTheme('dark')">🌙 深色模式</button>
<button onclick="handleSwitchTheme('blue')">💙 蓝色主题</button>
<button onclick="handleSwitchTheme('green')">💚 绿色主题</button>
</div>
<h2>调整主题变量</h2>
<div class="theme-control">
<div>
<label for="backgroundColor">背景色:</label>
<input type="color" id="backgroundColor" onchange="setThemeVariable('--background-color', this.value)">
</div>
<div>
<label for="primaryColor">主色:</label>
<input type="color" id="primaryColor" value="#bb86fc" onchange="setThemeVariable('--primary-color', this.value)">
</div>
<div>
<label for="textColor">文字色:</label>
<input type="color" id="textColor" onchange="setThemeVariable('--text-color', this.value)">
</div>
<div>
<label for="borderColor">边框色:</label>
<input type="color" id="borderColor" onchange="setThemeVariable('--border-color', this.value)">
</div>
<div>
<label for="accentColor">强调色:</label>
<input type="color" id="accentColor" onchange="setThemeVariable('--accent-color', this.value)">
</div>
<div>
<label for="shadowColor">阴影色:</label>
<input type="color" id="shadowColor" onchange="setThemeVariable('--shadow-color', this.value)">
</div>
<div>
<label for="successColor">成功色:</label>
<input type="color" id="successColor" onchange="setThemeVariable('--success-color', this.value)">
</div>
<div>
<label for="warningColor">警告色:</label>
<input type="color" id="warningColor" onchange="setThemeVariable('--warning-color', this.value)">
</div>
<div>
<label for="errorColor">错误色:</label>
<input type="color" id="errorColor" onchange="setThemeVariable('--error-color', this.value)">
</div>
</div>
<div>
<h2>当前主题</h2>
<p>当前主题:<span id="themeName" class="current-theme">loading...</span></p>
</div>
</body>
</html>