如何隐藏 DIV,它们在页面加载时瞬间显示
我制作了一个非常快速的 jQuery 幻灯片,我用它来隐藏在轮到它们之前不应该显示的 DIV:
$(document).ready(function() {
//Hide All Div's Apart From First
$('div#gall2').hide();
$('div#gall3').hide();
$('div#gall4').hide();
但是在加载页面时,您可以在它们显示之前瞬间看到 DIV 的 gall2 gall3 和 gall4隐。
在我的 CSS 中添加可以吗:
#gall2, #gall3, #gall4{ display:none;}
这将解决它们显示一瞬间的问题,只是想知道它是否可以接受
I have made a very quick jQuery slideshow, and I'm using this to hide the DIVs which shouldn't be shown until it's their turn:
$(document).ready(function() {
//Hide All Div's Apart From First
$('div#gall2').hide();
$('div#gall3').hide();
$('div#gall4').hide();
But on loading the page you can see DIV's gall2 gall3 and gall4 for a split second before they are hidden.
Would it be OK to just add inside my CSS:
#gall2, #gall3, #gall4{ display:none;}
This would solve the problem of them showing for a split second, just want to know if it's acceptable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 CSS 中禁用是可以的,但是没有 JS 的用户将永远不会看到它们。
要仅对使用 JS 的用户禁用,请标记正文和相应的 CSS:
CSS:
Disabling in CSS is fine, but then users without JS will never see them.
To disable for users with JS only, mark the body and appropriate CSS:
CSS:
作为 orip 答案的替代方案,如果您不喜欢在
中添加脚本,您可以执行以下操作:
您将避免这种方式闪烁,因为您正在向 HTML 添加类名在 DOM 准备好之前。
As an alternatie to orip's answer, you can do the following if you don't like adding scripts in the
<body>
:You will avoid flickering this way because you are adding a class name to the HTML before DOM ready.
是的,在 CSS 中这样做当然是可以接受的其他受访者是正确的。仅使用 JS 使事物可见并不是一个好习惯。我想我只是在技术上思考:以这种方式改变你的 CSS 可以解决这个问题。
Yes, it's certainly acceptable to do it in your CSSThe other respondents are correct. It's not a good practice to make things only visible with JS. I guess I was just thinking technically : changing your CSS this way would solve the issue.
总是尝试想象关闭 JS 的用户会看到什么。如果您使用 CSS 解决方案,则也不会为他们显示非第一个 DIV,但如果您以编程方式更改幻灯片,显然他们只会看到第一个 DIV。最好给他们一个“无 JS”选项。然后,如果您可以确定用户启用了 JS,则可以使用 CSS 隐藏 div 并使用 JS 显示它们。
Always try to imagine what users who have JS turned off will see. If you use the CSS solution, the non-first DIVs will not be shown for them either, but if you change slides programmatically, they will see only the first one, obviously. It's best to give them a "No JS" option. Then, if you can be sure the user has JS enabled, you can hide the divs using CSS and show them using JS.
它们被显示是因为默认情况下 div 可能被设置为显示。由于在页面上触发“ready”事件可能需要几秒钟的时间,因此在页面上的所有内容都加载并准备就绪之前,.hide() 方法调用不会命中。
如果我没记错的话, .hide() 方法只会将元素的“display”属性设置为“none”。因此,默认情况下通过 CSS 将 div 设置为隐藏可以让它们始终隐藏,并且您可以获得用户的额外好处,而 JS 也看不到它们。
They are being shown because on default the divs are likely set to be shown. Since it can take a few seconds for the "ready" event to be fired on the page, the .hide() method calls won't hit until everything on the page is loaded and ready.
If I remember right the .hide() method will just set the "display" attribute of the element to "none". So by default setting the div to be hidden through CSS allows them to always be hidden, and you get the added bonus of users without JS not seeing them either.