尾风暗模式在页面上一直滚动时不完全延伸
在NextJS Web应用程序中使用Tailwind的Dark Mode,在滚动时,如果您滚动经过滚动容器(滚动时几乎就像您的底部或顶部弹跳),则暗模式并非一路延伸,所以颜色不应用,它只是下面的先前颜色(在这种情况下为白色),这是什么原因,有没有办法完全扩展黑模式?
不起作用的浏览器
- 黑暗模式下
- 在
- stackoverflow和tailwindcss.com的
处理
- Safari
浏览器在黑暗模式下处理此井,而暗模式在整个页面上完全延伸到整个页面
_APP.TSX
<Store state={state} dispatch={dispatch}>
<Head>
<meta charSet="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
</Head>
<div className="h-screen dark dark:bg-black dark:text-white overscroll-auto lg:overscroll-contain">
<Component {...pageProps} id="app" />
</div>
</Store>{" "}
using dark mode in a nextjs web app with tailwind, when scrolling, if you scroll past the scroll container (almost like your bouncing off the bottom or top of the page when scrolling), the dark mode isn't extending all the way, so the color isn't applying and it's just the previous color underneath (white in this case), what is the reason for this and is there a way to extend the dark mode fully?
Browsers that don't work
- Firefox
- Brave
- Chrome
Browsers that do work
- Safari
stackoverflow and tailwindcss.com in dark mode handle this well and the dark mode extends fully on the whole page
_app.tsx
<Store state={state} dispatch={dispatch}>
<Head>
<meta charSet="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
</Head>
<div className="h-screen dark dark:bg-black dark:text-white overscroll-auto lg:overscroll-contain">
<Component {...pageProps} id="app" />
</div>
</Store>{" "}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将样式应用于
body
或:root
(html
)元素。对于示例,我将向它们显示应用于:root
元素。您在Next.js中有两个主要选项 - 全局样式表或内联。
全球样式表,带有 tailwind Directives
Global styles.css global styles.css
global.css
intellline inthine类内线
以应用于内线,必须创建一个自定义 _文档页面。同样,您可以将样式应用于
body
或html
标签。_document.js
You must apply the styles to the
body
or:root
(HTML
) element. For the examples, I'll show them applied to the:root
element.You have two primary options in Next.js - global stylesheet or inline.
Global stylesheet with tailwind directives
Global styles
global.css
inline class
To apply styles inline, you must create a custom _document page. Again, you can apply the styles to either the
body
orhtml
tags._document.js
我遇到了同样的问题。事实证明,这是因为我的深色背景/前景颜色样式在一个反应组件(容器布局组件)上,而不是在身体元素上。
我通过将深色背景/前景直接设置在我的CSS文件中的身体元素上进行修复:
然后在您的页面/_app.jsx文件或任何地方都可以调用
docuct.docorme> document.documentEllement.classlist.classlist.add(“ dark”) ;
,即使在滚动时,也将正确设置暗模式。https://tailwindcss.com/docs/docs/dark-mode
I was running into the same problem. Turns out it was because my dark background/foreground color styling was on a React component (a container layout component) rather than being on the body element.
I fixed it by setting the dark background/foreground directly on the body element in my css file:
Then in your pages/_app.jsx file or wherever, you can call
document.documentElement.classList.add("dark");
and the dark mode will be set properly even on scroll.https://tailwindcss.com/docs/dark-mode
我尝试了所有以前的答案,但它们都不对我有用。唯一最终工作的是创建一个_document.js,然后在html部分中添加一个ID:
_document.js,
然后在切换过程中添加并添加并删除深色背景(没有“ dark:'prefix):
darkmode.jsx
I 。
sean w )让我朝着正确的方向设置我
I tried all of previous answers and none of them worked for me. The only thing that finally worked was to create a _document.js, and add an id to the HTML section:
_document.js
Then add and remove the dark background (without the 'dark:' prefix) during the toggle:
DarkMode.jsx
I did not add the tailwind directive to globals.css
Thanks to Sean W for setting me in the right direction.