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博客添加暗色模式

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

  1. hexo博客matery主题个性化配置

    hexo博客个性化配置 参考博客 CDN加速:在页脚底部添加又拍云图片logo 博客文章开头format格式 修改hexo new post格式的方法 hexo s手机端查看 修改博客标题在手机端的字 ...

  2. hexo博客添加暗色模式_我如何将暗模式添加到我的网站

    hexo博客添加暗色模式 I recently redesigned my website. Here are 2 pictures of how it looked, for reference: ...

  3. 利用Cloudflare为基于GitHub Pages的Hexo博客添加HTTPS支持

    文章目录 ● 前言 ● 注册 Cloudflare ● 添加站点 ● 修改DNS ● 开启 HTTPS ● 重定向强制 HTTPS HTTP(超文本传输协议),是一个基于请求与响应,无状态的,应用层的 ...

  4. 向Hexo博客添加微博秀(pug模板)

    目的 本文是一个详细的教程,告诉大家如何在Hexo博客中嵌入微博秀或者微博直播组件. 为什么要向博客中添加微博秀/微博直播组件? 微博秀是什么 新浪微博秀,可以放置在你的博客.网站,或是其它支持htm ...

  5. Hexo博客发表文章、草稿、添加分类和标签

    写在前面 本文主要写了hexo的配置,关于博客标题这些配置HEXO已经有了很详细的介绍了,这里不再赘述,本文主要记录一些我个人认为小白比较容易有疑问的地方,也就是博客的主要功能,发表文章.添加文章分类 ...

  6. hexo博客fluid主题添加aplayer组件

    aplayer是一个非常漂亮好用的HTML5音乐播放器,和dplayer师出同门.我用的主题是fluid,记录一下添加音乐插件的过程. 我的HEXO博客 安装aplayer npm和github两种方 ...

  7. hexo博客next主题添加对数学公式的支持

    hexo博客默认为markdown格式,但是新搭建的hexo博客不支持公式渲染,下面介绍hexo博客next主题下添加对数学公式支持的方法. 环境说明: hexo版本:5.4.1 next版本: 最新 ...

  8. Hexo 博客添加看板娘

    Hexo 博客添加看板娘 相信看到这里的小伙伴都有属于自己的hexo博客了吧,废话不多说,我们直接进入主题 我的博客:传送门 1.安装模块 在hexo根目录下用Git执行命令 npm install ...

  9. 为 hexo 博客添加本地搜索功能

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:我的大学到研究生自学 Java 之路,过程艰辛,不放弃,保持热情,最终发现我是这样拿到大厂 offer 的!个人 ...

最新文章

  1. Windows单机配置Zookeeper环境
  2. linux安装下载中文包,linux下安装中文包和字体
  3. jl1.如何设置元素的宽高包含元素的边框和内边距
  4. iOS 使用 Core Plot 绘制统计图表入门
  5. php 0 n随机数,PHP n个不重复的随机数生成代码
  6. 【图像融合】基于matlab IHS图像融合【含Matlab源码 724期】
  7. 华为手机USB调试与Log
  8. 汉语言01C1501,2019年北京自考汉语言文学(本科)专业(01C1501)要考哪些科目?...
  9. C++ 仿QQ聊天程序 QQ 聊天室 源码
  10. 第二章 计算机系统和计算机组成原理
  11. C++ push_back()和back() 、pop()、push()、emplace() 和 emplace_back()
  12. ※ 数值信息——除二取余
  13. 收藏:不错的数据中台建设方法论
  14. 哈罗要在网约车市场取得突破,离开补贴行不通
  15. 微信小程序配置多环境
  16. Java方法详解(基础)
  17. 2020年下半年软件设计师下午真题试题(案例分析)及答案
  18. 高等数学复习之导数的运算(等价无穷小替换)
  19. 翻译:Swarm intelligence群体智慧
  20. iOS —— 触摸事件传递及响应与手势

热门文章

  1. java开发岗位招聘,吊打面试官
  2. BZOJ1014: [JSOI2008]火星人prefix
  3. iOS:通信录(完成)(18-01-18更)
  4. 初识virtual memory
  5. 编程算法 - 将排序数组按绝对值大小排序 代码(java)
  6. iphone UITableView及UIWebView的使用
  7. Hadoop小知识点
  8. [Egret][文档]遮罩
  9. puppet puppet模块、file模块
  10. node:爬虫爬取网页图片