如何使图像和文本链接都链接到同一位置,并且在悬停时都变为活动状态,而与我悬停在哪一个上无关?

发布于 2024-08-04 13:02:58 字数 927 浏览 2 评论 0原文

我希望能够将鼠标悬停在图像上,从而调用其旁边的文本链接上的悬停属性。

我有一个视频的图像预览,以及在其旁边以文本字符串形式写的视频名称。我想这样做,这样我就可以将鼠标悬停在图片上并使文本仍然像悬停效果一样改变颜色,而不必将图像和文本全部变成一张图像,出于明显的搜索和可用性原因;将文本保留为文本等。

更新:我还想确保现在位于“跨度”中的文本链接不能仅使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-黛色若梦 2024-08-11 13:02:58

您可以使用 CSS 或 Javascript 来完成此操作。如果不知道您的标记以及元素的布局方式,就不可能确定哪个选项最好。

CSS 选项

a:hover span { color:red; }
<a href="1.html">
  <img src="1.jpg" />
  <span>Title: One</span>
</a>

在该示例中,我们的文本和图像都位于链接内。然后我们修改 :hover 事件上文本的颜色。

Javascript (jQuery)

div.hovered p { color:red; }

<div class="preview">
  <img src="1.jpg" />
  <p>Title: One</p>
</div>

$("div.preview img").hover(
  function () { $(this).parent().addClass("hovered"); },
  function () { $(this).parent().removeClass("hovered"); }
);

此示例展示了一个解决方案,该解决方案只需将一个类添加到父容器,该类又会修改标题的表示形式。该事件是通过将图像悬停在容器内来控制的。

更新:

定位文本并不那么困难。快速简单的方法是使用相对定位父容器,使用绝对定位文本:

<div class="container">
  <a href="#">
    <img src="1.jpg" />
    <span class="title">Hello World</span>
  </a>
</div>

div.container            { position:relative; width:150px; }
div.container span.title { position:absolute; top:0; left:50px; width:100px; }

这是一个简单的示例。您可以根据需要修改这些值。

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

a:hover span { color:red; }
<a href="1.html">
  <img src="1.jpg" />
  <span>Title: One</span>
</a>

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)

div.hovered p { color:red; }

<div class="preview">
  <img src="1.jpg" />
  <p>Title: One</p>
</div>

$("div.preview img").hover(
  function () { $(this).parent().addClass("hovered"); },
  function () { $(this).parent().removeClass("hovered"); }
);

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:

<div class="container">
  <a href="#">
    <img src="1.jpg" />
    <span class="title">Hello World</span>
  </a>
</div>

div.container            { position:relative; width:150px; }
div.container span.title { position:absolute; top:0; left:50px; width:100px; }

That's a quick example. You can modify the values to your needs.

来日方长 2024-08-11 13:02:58

怎么样

<style>
a 
{ 
    color: #000000; 
}
a:hover 
{ 
    color: #a9a9a9; 
}
</style>

<a href="#"><img src="imagepath" alt="Image Title" /> Image Text</a>

What about

<style>
a 
{ 
    color: #000000; 
}
a:hover 
{ 
    color: #a9a9a9; 
}
</style>

<a href="#"><img src="imagepath" alt="Image Title" /> Image Text</a>
月亮邮递员 2024-08-11 13:02:58

基于 Jonathan 的回答,但对于 javascript,如果您不使用 jQuery:

<div onmousemove="hover('text1',true)" onmouseout="hover('text1',false)" >
  <img src="1.jpg" />
  <p id="text1">Title: One</p>
</div>

<script type="text/javascript">
function hover(elemID, on){
  element = document.getElementById(elemID);  
  if(on){
    element.style.color='red';
  }
  else
  {
    element.style.color='black';
  }
}
</script>

Based on Jonathan's answer, but for javascript if you don't use jQuery:

<div onmousemove="hover('text1',true)" onmouseout="hover('text1',false)" >
  <img src="1.jpg" />
  <p id="text1">Title: One</p>
</div>

<script type="text/javascript">
function hover(elemID, on){
  element = document.getElementById(elemID);  
  if(on){
    element.style.color='red';
  }
  else
  {
    element.style.color='black';
  }
}
</script>
幼儿园老大 2024-08-11 13:02:58

我很抱歉。我读错了你的问题。这是另一个尝试:

HTML

<div class="video">
    <a href="" title="title">
        <img alt="title" src="path/to/image" />
        Video Title
    </a>
</div>

CSS

    .video {
        line-height: 75px;
        margin: 10px;
        padding: 10px;
        width: 200px;   
    }
    .video img {
        float: left;
        margin-right: 15px; 
    }
    .video a:hover {
        color: #hexcol; 
    }
    .video:after {
        clear: both;
        content: ".";
        display: block;
        height: 0;
        visibility: hidden; 
    }

显然,这里有一些您不需要的样式,但我用它们在浏览器中进行了测试。如果您希望文本与视频缩略图垂直对齐,则需要将 line-height 属性设置为与缩略图相同的高度。这会将文本推到视频的中间。

My apologies. I mis-read your question. Here is another stab at it:

HTML

<div class="video">
    <a href="" title="title">
        <img alt="title" src="path/to/image" />
        Video Title
    </a>
</div>

CSS

    .video {
        line-height: 75px;
        margin: 10px;
        padding: 10px;
        width: 200px;   
    }
    .video img {
        float: left;
        margin-right: 15px; 
    }
    .video a:hover {
        color: #hexcol; 
    }
    .video:after {
        clear: both;
        content: ".";
        display: block;
        height: 0;
        visibility: hidden; 
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文