css/javascript 的可见设置
我想让我的 html 的一部分(导航栏类型对象,如果有影响的话)不可见,直到我将鼠标悬停在页面上的某个位置上。然后,我希望能够将鼠标悬停在使其可见的按钮上,让导航栏弹出,然后能够访问导航栏中的项目而不消失。有人有任何指点吗?我想编写一个 javascript 函数来执行此操作,但我不确定如何使事物不可见/可见并使用 javascript 切换该状态。
谢谢
I'd like to make a section of my html (a nav bar type object, if it makes a difference) invisible, until I hover over a certain place on the page. Then, I'd like to be able to hover over the button that makes it visible, have the nav bar pop into existence, and then be able to access items in the nav bar without it disappearing. Anyone have any pointers? I'd like to maybe write a javascript function to do this, but I'm not sure how to go about making things invisible/visible and toggling that state with javascript.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需创建一个隐藏的导航栏并将 onmouseover 函数添加到按钮:
和按钮:
如果导航栏要出现在按钮上方或紧邻按钮,您还可以研究全 css 解决方案,但您必须确保鼠标在移动到导航栏时永远不必“离开”按钮(只要您可以到达导航栏,即使导航栏大于按钮/位于按钮顶部) div,没有离开按钮 div 就可以正常工作了):
Just create a hidden navbar and add an onmouseover function to the button:
and the button:
If the navbar is going to appear over or immediately adjacent to the button, you could also look into an all css solution, but you've have to ensure that your mouse would never have to "leave" the button are while traveling to the navbar (as long as you can make it to the navbar, even if the navbar is larger than/on top of the button div, without leaving the button div it will work fine):
如果您只想创建一个基本的悬停菜单,则根本不需要 JavaScript,只需使用 CSS 即可。
这是一个 jsfiddle 演示,演示如何使用 CSS 创建悬停菜单。
要直接回答您的问题,您可以使用 JavaScript 操作
display
或visibility
CSS 值,如下所示:display
和之间的区别可见性归结为您希望如何处理不可见元素。通过使用
display
属性,元素将不占用页面中的空间,它将具有 0 的高度和宽度,并且填充或边距不会对其产生影响。另一方面,visibility
属性仅意味着您看不到元素的内容(文本、子元素等),但它仍会占用 DOM 中的空间,因此其他元素将会移动围绕它。这对于隐藏/显示内容很有用,但当所有内容再次变得可见时,页面内容不会移动。If you're just wanting to create a basic hover menu you don't need JavaScript at all you can get away with just CSS.
Here's a jsfiddle demo of how to use CSS to create a hover menu.
To directly answer your question you can either manipulate the
display
or thevisibility
CSS value using JavaScript like so:The different between
display
andvisibility
comes down to how you want to have the invisible element treated. By using thedisplay
property the element will take up no space in the page, it will have a 0 height and width and padding or margin wont effect it. Thevisibility
property on the other hand just means that you can't see the contents of the element (text, child elements, etc) but it will still take up space in the DOM so other elements will move around it. This can be useful to hide/ show content but not have the page contents move around as everything becomes visible again.如果您使用 jQuery,您可以执行以下操作:
当您将鼠标悬停在触发元素上时,此代码会显示导航栏。然后,当您将鼠标移出触发器时,它会等待 4.5 秒,然后再次隐藏导航栏。然后,当您将鼠标悬停在导航栏上时,第二部分会取消先前隐藏导航栏的调用,然后在鼠标移出导航栏 1.5 秒后将其隐藏。调整超时以适合您的口味和 IxD 需求。超时允许用户继续使用这些元素,而不会因为意外地将鼠标移出导航栏区域而受到惩罚。
If you use jQuery, you could do something like:
This code shows the navbar when you mouse over the trigger element. Then, when you mouse out of the trigger, it waits 4.5s before hiding the navbar again. Then the second part cancels the previous call to hide the navbar when you mouse over the navbar and then hides it 1.5s after you mouse out of the navbar. Adjust timeouts to suit your taste and IxD needs. The timeouts allow the user to continue using the elements and not be penalized for accidentally mousing out of the navbar area.
最可重用的方法是使用一个 css 类,添加到元素中以将其标记为隐藏,或者删除以显示元素
css:
javascript:
The most reusable way to do it, would be to use a css class that you add to the element to mark it hidden or that you remove to show the element
css:
javascript:
如果你想用一些动画来完成,你可以使用缓动插件
you can use easing plugin if you want it to be done with some animation