要根据用户机器的本地时间将Webflow的主CSS交换到其他GitHub托管的CSS,可以通过JavaScript来实现。以下是一个详细的步骤和示例代码:
假设你在GitHub上有两个CSS文件:
main-day.css
(白天样式)main-night.css
(夜间样式)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic CSS</title>
<link id="main-css" rel="stylesheet" href="https://raw.githubusercontent.com/yourusername/yourrepo/main-day.css">
</head>
<body>
<h1>Welcome to My Website</h1>
<script src="script.js"></script>
</body>
</html>
document.addEventListener("DOMContentLoaded", function() {
const cssLink = document.getElementById('main-css');
const currentHour = new Date().getHours();
if (currentHour >= 6 && currentHour < 18) {
// Daytime
cssLink.href = "https://raw.githubusercontent.com/yourusername/yourrepo/main-day.css";
} else {
// Nighttime
cssLink.href = "https://raw.githubusercontent.com/yourusername/yourrepo/main-night.css";
}
});
<head>
中定义了一个<link>
标签,初始指向白天的CSS文件。script.js
。DOMContentLoaded
事件确保DOM完全加载后再执行脚本。通过这种方式,你可以根据用户的本地时间动态切换不同的CSS样式,提升用户体验。
领取专属 10元无门槛券
手把手带您无忧上云