多悬停突出显示
我有一个想法,一种给用户反馈的方法 我喜欢将鼠标悬停在菜单上,这将突出显示与菜单对应的 img 或者相反,将鼠标悬停在图像上将突出显示菜单
我认为它可以使用 jquery 完成,但是可以用纯 css 完成吗?或者你有一个示例或代码吗?我可以基于我的想法
谢谢
I have a idea, a way to give user feedback
i like to hover on a menu, that will highlight an img correcponding to the menu
OR theinverse, hover over image will highlight the menu
I think it can be done with jquery, but can it be done in pure css or do you have a example or code i can base my idea on
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,据我所知,在 jQuery 中,您有一个菜单和一些图像:
首先,您需要一些东西来链接两者,例如类或 rel 引用
然后使用 jQuery,您可以在翻转时向元素添加一个类
您实际上可以使用任何东西使用 .text() 或 .atrr('src') 作为标识符
高亮类将包含翻转的样式,对于使用 li .hightlight 和 img .highlight 或使用不同的类的菜单和图像来说,这些样式显然可能不同。
So in jQuery as far as I understand you have a menu and some images:
First you need something to link the two, such as a class or rel reference
Then using jQuery you could add a class to the elements on rollover
You could use anything really as an indentifyer using .text() or .atrr('src')
The highlight class would contain the styles for your rollover, which could obviously be different for the menu and image either with li .hightlight and img .highlight or using different classes.
假设您可以在图像上使用绝对定位,则无需 JavaScript 即可完成此操作。只需将
img
嵌套在元素内(大概是一个锚点,否则在 IE6 中不起作用),将其绝对定位(在各个菜单项上使用 ID 来以不同方式定位图像)并添加悬停样式到锚点。悬停操作适用于绝对定位的img
和包含的a
元素。这是一个非常简单的例子:
Assuming you can use absolute positioning on the image, you can do this without Javascript. Just nest the
img
inside the element (presumably an anchor, otherwise won't work in IE6), position it absolutely (use IDs on the various menu items to position the images differently) and add a hover style to the anchor. The hover action will work for both the absolutely positionedimg
and the containinga
element.Here is a very simple example:
只要您要突出显示的图像是您悬停在其上的项目的同级图像或后代图像,就可以相对轻松地完成。
Siblings
Descendants
相反(将鼠标悬停在图像上以显示菜单)对于 CSS 来说是不可能的,因为级联是单向的。虽然用 JavaScript 实现相对容易,但我只能提供 jQuery(令我羞愧):
我很好奇为什么你想采用这种方法,似乎你想将鼠标悬停在项目(在本例中为“缩略图”和“全尺寸”)来显示另一个?这意味着这些项目中的一个或两个将同时不可见/隐藏。在这种情况下,用户如何知道他们的存在以进行交互?
演示位于 JS Bin (基于兄弟姐妹)。
对于两者之间的“页面上的任何位置”关系,这是另一种选择:
演示:位于 JS Bin。
html:
它与 @BenWells' 有点不同,因为它不需要明确的关系,但它确实要求
缩略图
与其对应的fullsize
元素的顺序相同(或反之亦然),因为它是基于他们的指数位置。As long as the image you want to highlight is sibling or descendant of the item you're hovering over, it can be done relatively easily.
Siblings
Descendants
For the reverse (to hover over the image to show the menu) isn't possible with CSS, since the cascade is one-way. It's relatively easy to implement with JavaScript though, but I can only offer jQuery (to my shame):
I am curious as to why you want to take this approach, it seems that you want to hover over either item (in this case 'thumbnail' and 'fullsize') to show the other? This implies that one, or both, of these items will at the same time be invisible/hidden. This being the case how does the user know they exist for interaction to take place?
Demo at JS Bin (based on siblings).
Here's an alternative, for an 'anywhere on the page' relationship between the two:
Demo: at JS Bin.
html:
It's a little different to @BenWells', in that it doesn't require an explicit relationship, but it does require that the
thumbnails
be in the same order as their counterpartfullsize
elements (or vice/versa) since it's based on their index positions.