Ben*_*nji 5 javascript reactjs tailwind-css react-hooks darkmode
我环顾四周,但找不到任何相关的问答。我正在使用 tailwindCSS 在 ReactJS 中构建一个项目,并在网站上实现暗模式。一切正常,但现在我的背景图像遇到了一些问题。我已将两个图像设置为tailwind.config.js
darkMode: 'class',
theme: {
extend: {
backgroundImage: (theme) => ({
'code': "url('/src/components/About/coding-bg-dark.png')",
'light-code': "url('/src/components/About/lightcode.png')",
})
},
},
Run Code Online (Sandbox Code Playgroud)
并将类名放在合适的部分
<section id='introduction' className="bg-code dark:bg-light-code bg-cover bg-fixed flex flex-wrap content-center w-full md:h-screen">
Run Code Online (Sandbox Code Playgroud)
但是当我切换深色模式时,图像不会改变,深色图像保持在浅色模式。知道我该怎么走吗?
您需要添加darkMode: 'media'并tailwind.config.js扩展背景图像的变体以包括暗模式。这是一个例子tailwind.config.js:
module.exports = {
darkMode: 'media',
theme: {
extend: {
backgroundImage: (theme) => ({
'image-one':
"url('https://images.unsplash.com/photo-1629651480694-edb8451b31aa?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=668&q=80')",
'image-two':
"url('https://images.unsplash.com/photo-1629651726230-6430554a8890?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2734&q=80')",
}),
},
},
variants: {
extend: {
backgroundImage: ['dark'],
},
},
plugins: [],
}
Run Code Online (Sandbox Code Playgroud)
那么它应该可以工作。您可以在此处查看一个工作示例。