如何使图像和文本链接都链接到同一位置,并且在悬停时都变为活动状态,而与我悬停在哪一个上无关?
我希望能够将鼠标悬停在图像上,从而调用其旁边的文本链接上的悬停属性。
我有一个视频的图像预览,以及在其旁边以文本字符串形式写的视频名称。我想这样做,这样我就可以将鼠标悬停在图片上并使文本仍然像悬停效果一样改变颜色,而不必将图像和文本全部变成一张图像,出于明显的搜索和可用性原因;将文本保留为文本等。
更新:我还想确保现在位于“跨度”中的文本链接不能仅使用“margin-left”移动,而是可以抬起。默认情况下,在代码中我让它下降到 60x60px 图像底部的水平。我希望它更高,这样看起来不错,但“边缘底部”无济于事。我希望它位于 60x60 img 的“中心高度”...而不是默认位置的底部。
有什么想法吗?谢谢!
ps 我对这里发布的 java 脚本和 jquery 想法持开放态度,但我对此完全是新手,需要额外的解释。我更喜欢CSS。
我现在的代码是:
我的html是:
<div id="example">
<a href="#">
<img src="image/source_60x60px.jpg">
<span class="video_text">Image Text</span>
</a>
</div>
我的css是:
div#example { float:left; width:358px; height:60px; margin-top:20px; }
div#example a { color:#B9B9B9; width:100%;}
div#example a:hover { color:#67a; text-decoration:none;}
span.videotext { margin-left:80px;}
I would like to be able to have hovering over the image invoke the hover property on the text link too that is next to it.
I have a image preview of a video and the video name written next to it as text string. I'd like to make it so i can hover on the picture and make the text still change color like a hover effect without having to make the image and text all one image , for obvious search and usability reasons; keeping text as text etc.
UPDATE: I also want to make sure the text link that is now in the "span" can not just move over using "margin-left" but can be raised up. By default in the code i have it just drops to the level of the bottom of the 60x60px image. I want it higher up so it looks nice but "margin-bottom" doesn't help that. i want it in the "Center height" of the 60x60 img... not at the bottom of it where it goes by default.
ANy ideas? thanks!
p.s. I'm open to the java script and jquery ideas some posted here but i am a complete newbie at that and will need extra explanation. I prefer CSS.
my code now is:
my html is:
<div id="example">
<a href="#">
<img src="image/source_60x60px.jpg">
<span class="video_text">Image Text</span>
</a>
</div>
my css is:
div#example { float:left; width:358px; height:60px; margin-top:20px; }
div#example a { color:#B9B9B9; width:100%;}
div#example a:hover { color:#67a; text-decoration:none;}
span.videotext { margin-left:80px;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 CSS 或 Javascript 来完成此操作。如果不知道您的标记以及元素的布局方式,就不可能确定哪个选项最好。
CSS 选项
在该示例中,我们的文本和图像都位于链接内。然后我们修改 :hover 事件上文本的颜色。
Javascript (jQuery)
此示例展示了一个解决方案,该解决方案只需将一个类添加到父容器,该类又会修改标题的表示形式。该事件是通过将图像悬停在容器内来控制的。
更新:
定位文本并不那么困难。快速简单的方法是使用相对定位父容器,使用绝对定位文本:
这是一个简单的示例。您可以根据需要修改这些值。
You could do this with CSS, or with Javascript. Without knowing your markup, and how your elements are laid out, it's not possible to determine which option is best.
CSS Option
In that example, we have our text and image both within a link. We are then modifying the color of the text on the :hover event.
Javascript (jQuery)
This example showcases a solution that simply adds a class to the parent container, which in turn modifies the representation of the title. This event is controlled by hovering the image within the container.
Update:
Positioning your text isn't that difficult. The quick-and-easy method would be to position the parent container with relative, and the text with absolute:
That's a quick example. You can modify the values to your needs.
怎么样
What about
基于 Jonathan 的回答,但对于 javascript,如果您不使用 jQuery:
Based on Jonathan's answer, but for javascript if you don't use jQuery:
我很抱歉。我读错了你的问题。这是另一个尝试:
HTML
CSS
显然,这里有一些您不需要的样式,但我用它们在浏览器中进行了测试。如果您希望文本与视频缩略图垂直对齐,则需要将 line-height 属性设置为与缩略图相同的高度。这会将文本推到视频的中间。
My apologies. I mis-read your question. Here is another stab at it:
HTML
CSS
Obviously, there are some styles in here that you don't need but I used them to test in a browser. If you want your text to vertically align with your video thumbnail, you will need to set the line-height property to the same height as the thumbnail image. That will push the text to the middle of the video.