jquery - 你如何根据用户的机器本地时间将 webflow 的主要 css 交换到其他 Github 托管 css?
问题描述
我正在尝试使用以下脚本根据用户本地时间将 css 应用于网站:
<script>
function getStylesheet() {
var currentTime = new Date().getHours();
if (0 <= currentTime&¤tTime < 5) {
document.write("<link rel='stylesheet' href='https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/night.css' type='text/css'>");
}
if (5 <= currentTime&¤tTime < 16) {
document.write("<link rel='stylesheet' href='portfolio-c16909.webflow.a39b8eeda.css' type='text/css'>");
}
if (16 <= currentTime&¤tTime < 22) {
document.write("<link rel='stylesheet' href='https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/afternoon.css' type='text/css'>");
}
if (22 <= currentTime&¤tTime <= 24) {
document.write("<link rel='stylesheet' href='https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/night.css' type='text/css'>");
}
}
getStylesheet();
</script>
<noscript><link href="portfolio-c16909.webflow.a39b8eeda.css" rel="stylesheet"></noscript>
基本上,我想做的是应用基于用户本地机器时间的 CSS 主题(例如,白天、下午和夜间类型的主题)。
这是我的原始 css,取自我的 repo @ GitHub(我知道下午.css 与 webflow 的主要 css 完全相同,但这很好,因为我稍后会修改它。我的主要观点是首先让脚本成功运行)。
非常感谢您的想法——非常感谢!
解决方案
我会按照下面的方式来做。<noscript>
如果您在脚本上方使用默认样式,则不需要。我不确定你的时间逻辑,也不想花太多时间在上面。
我还添加了一个间隔调用,以便当有人在页面上时样式会发生变化,并且时间恰好会结束。它仅每小时运行一次,因此可能会模糊约 59 分钟。
<link id="myDynamicStyles" href="portfolio-c16909.webflow.a39b8eeda.css" rel="stylesheet">
<script>
function changeStyleSheets() {
const myDynamicStyleTag = document.getElementById('myDynamicStyles');
const currentTime = new Date().getHours();
if( currentTime < 5 ) {
myDynamicStyleTag.href = 'https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/night.css';
} else if( currentTime < 16 ) {
myDynamicStyleTag.href = 'portfolio-c16909.webflow.a39b8eeda.css';
} else if( currentTime < 22 ) {
myDynamicStyleTag.href = 'https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/afternoon.css';
} else if( currentTime <= 24 ) {
myDynamicStyleTag.href = 'https://rawcdn.githack.com/Jacopospina/humansplusmachines/6647fd1cb01b9b756e11d50f88efd72a6e800694/night.css';
}
}
changeStyleSheets();
setInterval(changeStyleSheets, 1000*60*60); // call this every hour
</script>
<h1>My headline</h1>
推荐阅读
- java - 如何使用 Rapid API 中的 LanguageTool API 检查 Java 中的字符串?
- c# - 将 Xamarin 应用注册页面连接到 MySQL 的 Azure 数据库
- mysql - 如果我有一个二级索引表和一个外键,是否为每个唯一外键创建索引?
- reactjs - 嗨,我正在尝试根据状态代码显示错误消息,但不幸的是在控制台中得到响应,而不是在客户端显示上
- flutter - 无法使用其官方 API 获取技术部分的 nytimes 数据
- javascript - 图表 JS 显示没有任何数据的空图表
- c# - 不能在 COSMOS DB 中使用没有鉴别器字段的实体框架进行 CRUD?
- python - 避免覆盖excel文件并用pandas创建一个新名称
- export - 搅拌机导出 FBX 失败
- javascript - Highcharts:极坐标图在动态加载的数据中无法正确显示系列