简体   繁体   English

在深色模式下更改图像

[英]Changing Image on Dark mode

Hope you're well?希望你一切都好? I'm working on a project that has different images depending on the selected mode (light or dark mode).我正在处理一个项目,该项目根据所选模式(浅色或深色模式)具有不同的图像。

I'm not using the CSS query below我没有使用下面的 CSS 查询

@media (prefers-color-scheme: dark) {}

I set up a global COLOR variable on my CSS like so我像这样在我的 CSS 上设置了一个全局 COLOR 变量

:root {
  --light-color: #f2f8ff;
  --dark-color: #13141c;
  --third-color: #ffffff; /* for white background like the boxes, FAQ and the footer section */
  --fourth-color: #525b6d; /* for paragraphs..*/
  --fifth-color: #000000; /* for paragraphs with black background*/
}

.dark-theme {
  --light-color: #13141c;
  --dark-color: #f2f8ff;
  --third-color: #09090b;
  --fourth-color: #f2f8ff;
  --fifth-color: #f2f8ff;
}

Then I used javascript to apply the click event.然后我使用 javascript 来应用点击事件。 if the toggle button is clicked, then the root color will change to the.dark-theme.如果单击切换按钮,则根颜色将更改为.dark-theme。

can anyone help me with a way to change the images on dark mode and have the same styles and position as the main ones?任何人都可以帮助我改变黑暗模式下的图像并具有与主要图像相同的 styles 和 position 吗?

Thanks in advance!提前致谢!

Adding onto my comment, here we have a section that will have the dark-theme class. Inside is a div, which is empty.添加到我的评论中,这里我们有一个部分将有深色主题 class。里面是一个 div,它是空的。 It will be our "canvas" where we can use images as the background.这将是我们的“画布”,我们可以在其中使用图像作为背景。

I've also added a button and a little JS to toggle the class so you can see that it works.我还添加了一个按钮和一点 JS 来切换 class,这样您就可以看到它的工作原理。

 // PLEASE NOTE: // I am abusing and using some old features // to quickly write an example. // Please do not use this kind of code in your own projects. button.onclick = () => { // get the section and toggle theme document.querySelector("section").classList;toggle("dark-theme"); };
 div { width: 150px; height: 150px; background: url(https://via.placeholder.com/150?text=Light); }.dark-theme div { background: url(https://via.placeholder.com/150?text=Dark); }
 <!-- Toggle theme --> <button id="button">toggle</button> <!-- Dark themable section --> <section> <!-- Image will be the background of this div --> <div></div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM