hexo博客添加暗色模式_我如何向网站添加暗模式

hexo博客添加暗色模式

同一个网站,两种不同的配色方案 (Same website, two different color schemes)

Last year I made it a point to redesign my website from scratch. I wanted something simple and minimalist looking that clearly stated what this was — a portfolio website.

去年,我特别指出了从头开始重新设计网站的重要性。 我想要一个简单而简约的外观,清楚地说明这是什么-一个投资组合网站。

After I rebuilt my website from the ground up it seemed like everywhere I turned there was another article about adding a dark mode to your website.

从头开始重新构建网站后,似乎到处都是转弯,还有另一篇关于向您的网站添加暗模式的文章。

At first I didn’t think it would make that big of a difference because, while I am partial to darker colors, I felt like my website was a good balance between bright, fun colors and darker fonts.

起初我不认为这会带来太大的改变,因为虽然我偏爱深色,但我觉得我的网站在明亮,有趣的色彩和深色字体之间取得了很好的平衡。

I read some of the articles I mentioned earlier and the more I thought about it the more I decided to go for it.

我读了我之前提到的一些文章,对它的思考越深,我决定就越去追求它。

I took some inspiration from Flavio Copes who wrote a terrific article on this very subject. Unlike what Flavio decided to do with his site, I didn’t add the user’s choice to local storage.

我从弗拉维奥·科普斯(Flavio Copes)那里获得了一些启发,他在这个主题上写了一篇很棒的文章 。 与Flavio决定使用其网站不同,我没有将用户的选择添加到本地存储中。

This is due, in part, to the differences between our sites. I have a static site and there are no redirects/separate pages aside from the blog which is on a different platform so users generally won’t be refreshing the page. It is a neat option and one that I may add in later on.

这部分是由于我们站点之间的差异。 我有一个静态网站,除了博客(位于另一个平台上)之外,没有重定向/单独的页面,因此用户通常不会刷新页面。 这是一个很好的选择,稍后我可以添加。

Ok, let’s dig into the nuts and bolts of how I accomplished my dark mode toggle.

好的,让我们深入研究如何完成黑暗模式切换。

代码 (The Code)

The code to achieve this was fairly simple. I took the same approach as Flavio did and added the style changes through CSS. I had to take a few more steps because I have an image on my landing page.

实现此目的的代码非常简单。 我采用了与Flavio相同的方法,并通过CSS添加了样式更改。 我必须再执行几步,因为我的目标网页上有一张图片。

I had to use the !important flag on some of the rules because they were not being applied properly. This was the easiest approach to implement and I know it’s not advised to use this flag so I’ll be looking for an alternative in the near future.

我必须在某些规则上使用!important标志,因为它们未正确应用。 这是最简单的实现方法,我知道不建议使用此标志,因此我会在不久的将来寻找替代方法。

Here is the JavaScript I used to get my toggle switch working correctly:

这是我用来使切换开关正常工作JavaScript:

I start by selecting my div with an id of light-dark-mode-container and adding an event listener to it.

我首先选择ID为light-dark-mode-container div ,然后向其添加事件监听器 。

Next, I set my variables of everything, which selects all content on the page, and projectTiles because this class belongs to a particular set of overlays I do not want to have a background of a solid color.

接下来,设置我的everything变量,该变量将选择页面上的所有内容,并选择projectTiles因为此类属于特定的一组叠加层,所以我不想使用纯色背景。

Next, since I’m using querySelectorAll which returns a static NodeList, I loop over all of the elements within both NodeLists and either toggle the class dark or completely remove it from the elements returned by the variable projectTiles.

接下来,由于我使用的是返回静态NodeList的 querySelectorAll ,因此我遍历了两个NodeList中的所有元素,然后将类切换为dark或将其从变量projectTiles返回的元素中完全删除。

What I’m left with is a container at the top of my website with a toggle switch that allows the user to toggle between light and dark mode.

我剩下的是网站顶部的容器,其中有一个切换开关,允许用户在亮和暗模式之间切换。

I hope you enjoyed this post and maybe you learned something too! If you decide to implement this on your own website or your next project please share it with me (leave me a comment or shout at me on Twitter). I’m always happy to see the work and projects that others create.

我希望您喜欢这篇文章,也许您也学到了一些东西! 如果您决定在自己的网站或下一个项目中实施此操作,请与我分享(在Twitter上给我留言或大喊大叫)。 我总是很高兴看到其他人创建的工作和项目。

This post was posted on my blog where I write articles related to front end web development. I also cross post over at Dev.to, so if you’re on that platform you can find my work as well!

这篇文章发布在我的博客上,我在那里写有关前端Web开发的文章。 我也在Dev.to上发布了文章 ,因此,如果您在该平台上,也可以找到我的作品!

While you’re there why not sign up for my Newsletter? I promise I’ll never spam your inbox and your information will not be shared with anyone else.

当您在那里时,为什么不注册我的时事通讯 ? 我保证我永远不会向您的收件箱发送垃圾邮件,并且不会与其他任何人共享您的信息。

Have an awesome day filled with love, joy, and coding!

美好的一天充满爱,欢乐和编码!

翻译自: https://www.freecodecamp.org/news/how-i-added-dark-mode-to-my-website-484bc3cb0318/

hexo博客添加暗色模式

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391623.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 331. 验证二叉树的前序序列化

序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时,我们可以记录下这个节点的值。如果它是一个空节点,我们可以使用一个标记值记录,例如 #。_9_/ \3 2/ \ / \4 1 # 6 / \ / \ / \ # # # # # # 例如&#xff0…

mongodb数据可视化_使用MongoDB实时可视化开放数据

mongodb数据可视化Using Python to connect to Taiwan Government PM2.5 open data API, and schedule to update data in real time to MongoDB — Part 2使用Python连接到台湾政府PM2.5开放数据API,并计划将数据实时更新到MongoDB —第2部分 目标 (Goal) This ti…

4.kafka的安装部署

为了安装过程对一些参数的理解,我先在这里提一下kafka一些重点概念,topic,broker,producer,consumer,message,partition,依赖于zookeeper, kafka是一种消息队列,他的服务端是由若干个broker组成的,broker会向zookeeper,producer生成者对应一个…

javascript初学者_针对JavaScript初学者的调试技巧和窍门

javascript初学者by Priyanka Garg由Priyanka Garg My intended audience for this tutorial is beginner programmers. You’ll learn about frustration-free debugging with chrome dev tools.本教程的目标读者是初学者。 您将学习使用chrome开发工具进行无挫折的调试。 D…

leetcode 705. 设计哈希集合

不使用任何内建的哈希表库设计一个哈希集合(HashSet)。 实现 MyHashSet 类: void add(key) 向哈希集合中插入值 key 。 bool contains(key) 返回哈希集合中是否存在这个值 key 。 void remove(key) 将给定值 key 从哈希集合中删除。如果哈希…

ecshop 前台个人中心修改侧边栏 和 侧边栏显示不全 或 导航现实不全

怎么给个人中心侧边栏加项或者减项 在模板文件default/user_menu.lbi 文件里添加或者修改,一般看到页面都会知道怎么加,怎么删,这里就不啰嗦了 添加一个栏目以后,这个地址跳的页面怎么写 这是最基本的一个包括左侧个人信息,头部导航栏 <!DOCTYPE html PUBLIC "-//W3C//…

leetcode 706. 设计哈希映射

不使用任何内建的哈希表库设计一个哈希映射&#xff08;HashMap&#xff09;。 实现 MyHashMap 类&#xff1a; MyHashMap() 用空映射初始化对象 void put(int key, int value) 向 HashMap 插入一个键值对 (key, value) 。如果 key 已经存在于映射中&#xff0c;则更新其对应…

数据库语言 数据查询_使用这种简单的查询语言开始查询数据

数据库语言 数据查询Working with data is becoming an increasingly important skill in the modern workplace. 在现代工作场所中&#xff0c;处理数据已成为越来越重要的技能。 Data is no longer the domain of analysts and software engineers. With todays technology,…

面向对象编程思想-观察者模式

一、引言 相信猿友都大大小小经历过一些面试&#xff0c;其中有道经典题目&#xff0c;场景是猫咪叫了一声&#xff0c;老鼠跑了&#xff0c;主人被惊醒&#xff08;设计有扩展性的可加分&#xff09;。对于初学者来说&#xff0c;可能一脸懵逼&#xff0c;这啥跟啥啊是&#x…

typescript 使用_如何使用TypeScript轻松修改Minecraft

typescript 使用by Josh Wulf通过乔什沃尔夫(Josh Wulf) 如何使用TypeScript轻松修改Minecraft (How to modify Minecraft the easy way with TypeScript) Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Min…

Python:在Pandas数据框中查找缺失值

How to find Missing values in a data frame using Python/Pandas如何使用Python / Pandas查找数据框中的缺失值 介绍&#xff1a; (Introduction:) When you start working on any data science project the data you are provided is never clean. One of the most common …

监督学习-回归分析

一、数学建模概述 监督学习&#xff1a;通过已有的训练样本进行训练得到一个最优模型&#xff0c;再利用这个模型将所有的输入映射为相应的输出。监督学习根据输出数据又分为回归问题&#xff08;regression&#xff09;和分类问题&#xff08;classfication&#xff09;&#…

leetcode 54. 螺旋矩阵(递归)

给你一个 m 行 n 列的矩阵 matrix &#xff0c;请按照 顺时针螺旋顺序 &#xff0c;返回矩阵中的所有元素。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 输出&#xff1a;[1,2,3,6,9,8,7,4,5] 示例 2&#xff1a; 输入&#xff1a;matrix [[1,…

微服务架构技能

2019独角兽企业重金招聘Python工程师标准>>> 微服务架构技能 博客分类&#xff1a; 架构 &#xff08;StuQ 微服务技能图谱&#xff09; 2课程简介 本课程分为基础篇和高级篇两部分&#xff0c;旨在通过完整的案例&#xff0c;呈现微服务的开发、测试、构建、部署、…

phpstorm 调试_PhpStorm中的多用户调试

phpstorm 调试by Ray Naldo雷纳尔多(Ray Naldo) PhpStorm中的多用户调试 (Multi-User Debugging in PhpStorm) 使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy) “Er, wait a minute… Don’t you just use xdebug.remote_connect_back which has been introduced since …

Tableau Desktop认证:为什么要关心以及如何通过

Woah, Tableau!哇&#xff0c;Tableau&#xff01; By now, almost everyone’s heard of the data visualization software that brought visual analytics to the public. Its intuitive drag and drop interface makes connecting to data, creating graphs, and sharing d…

约束布局constraint-layout导入失败的解决方案 - 转

今天有同事用到了约束布局&#xff0c;但是导入我的工程出现错误 **提示错误&#xff1a; Could not find com.Android.support.constraint:constraint-layout:1.0.0-alpha3** 我网上查了一下资料&#xff0c;都说是因为我的androidStudio版本是最新的稳定版导入这个包就会报这…

算法复习:冒泡排序

思想&#xff1a;对于一个列表,每个数都是一个"气泡 "&#xff0c;数字越大表示"越重 "&#xff0c;最重的气泡移动到列表最后一位&#xff0c;冒泡排序后的结果就是“气泡”按照它们的重量依次移动到列表中它们相应的位置。 算法&#xff1a;搜索整个列表…

leetcode 59. 螺旋矩阵 II(递归)

给你一个正整数 n &#xff0c;生成一个包含 1 到 n2 所有元素&#xff0c;且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。 示例 1&#xff1a; 输入&#xff1a;n 3 输出&#xff1a;[[1,2,3],[8,9,4],[7,6,5]] 解题思路 按层进行数字的填充&#xff0c;每一层…

前端基础进阶(七):函数与函数式编程

纵观JavaScript中所有必须需要掌握的重点知识中&#xff0c;函数是我们在初学的时候最容易忽视的一个知识点。在学习的过程中&#xff0c;可能会有很多人、很多文章告诉你面向对象很重要&#xff0c;原型很重要&#xff0c;可是却很少有人告诉你&#xff0c;面向对象中所有的重…