简体   繁体   English

jQuery hover()在图像上

[英]Jquery hover() over image

I'm trying to make a functionality when a user hovers over an image then an anchor element will appear The user can then click on the anchor tag to follow the link. 当用户将鼠标悬停在图像上时,我将尝试创建一个功能,然后将出现一个锚元素。然后,用户可以单击锚标签以跟随链接。 When the user isn't hovering the image, it disappears. 当用户不悬停图像时,它会消失。

Making the anchor tag appear when I hover over the image works fine, but when I hover over the anchor tag, it disappears. 当我将鼠标悬停在图像上时,使锚标记出现会很好,但是当我将鼠标悬停在图像上时,它会消失。 The reason is because when the anchor element appears, the mouse is no longer hovering over the image directly. 原因是因为当锚元素出现时,鼠标不再直接悬停在图像上。 Soooo basically, my tactic doesn't work. 太基本了,我的策略不起作用。 Could someone suggest a method? 有人可以建议一种方法吗?

Here's my method below: http://jsfiddle.net/5z4RL/ 这是我下面的方法: http : //jsfiddle.net/5z4RL/

HTML // I have a div with an unordered list . HTML //我有一个无序列表div Each li has an img and an anchor 每个都有一个img和一个锚点

    <div id="img_container">

         <ul>
            <li>
                <img src="img/first.jpg" width="260px" height="260px">
                <a href="#">Click Here To Visit </a>
            </li>
            <li>
                   <img src="img/second.jpg" width="260px" height="260px">
                <a href="#"> Click Here To Visit </a>
            </li>
            <li>
                <img src="img/third.jpg" width="260px" height="260px">
                <a href="#"> Click Here To Visit </a>
            </li>
        </ul>

    </div>

JQUERY JQUERY

 $(document).ready(function(){ 
    $("#img_container img").hover(
      function(){
        $(this).next().css("visibility","visible");
       }, function(){
        $(this).next().css("visibility","hidden");
       }

    );

});// end of ready

You can just use LI instead: 您可以只使用LI来代替:

$("#img_container li").hover(
    function(){
        $(this).find('a').css("visibility","visible");
    }, function(){
        $(this).find('a').css("visibility","hidden");
    }      
);

Just use CSS for something as simple as this: 只需使用CSS这样简单的内容即可:

#img_container li a {
    /* current styles */
    visibility: hidden;
}
#img_container li:hover a {
    visibility: visible;
}

This hides the a elements by default then shows them when you hover over the parent li . 默认情况下,这会隐藏a元素,然后将鼠标悬停在父li时会显示它们。

Here it is working: http://jsfiddle.net/sPL73/ 它在这里工作: http : //jsfiddle.net/sPL73/

If you only want the a s to be visible when hovering over the image use this instead: 如果仅希望将悬停在图像上时显示a ,请使用以下代码:

#img_container li a {
    /* current styles */
    visibility: hidden;
}
#img_container img:hover + a, #img_container li a:hover {
    visibility: visible;
}

This uses CSS3's adjacent sibling selector to select the a next to an image that's being hovered over. 这使用CSS3的相邻的兄弟选择器以选择a旁边被真实悬停的图像。 Here's a demo: http://jsfiddle.net/4wcQr/ 这是一个演示: http : //jsfiddle.net/4wcQr/

Bind to the list elements instead of the image. 绑定到列表元素而不是图像。 Then fade the anchor tag in and out accordingly: 然后相应地淡入和淡出锚标签:

$(function(){ 
    $("#img_container li").hover(function () {
        $(this).find('a').fadeIn();
    }, function () {
        $(this).find('a').fadeOut();
    });
});

You can attach your hover handler on <li> , as already said, or alternatively attach it to both the <img> and <a> . 您可以将您的hover在处理<li>前面已经说了, 或者将其连接到两个<img><a>

This might be useful if you don't want to rely on the boundaries of the <li> element. 如果您不想依赖<li>元素的边界,这可能会很有用。

Here's a fiddle: http://jsfiddle.net/5z4RL/3/ 这是一个小提琴: http : //jsfiddle.net/5z4RL/3/

$(document).ready(function(){ 
    $("#img_container img, #img_container a").hover(
        function(){
            $(this).parent().find("a").css("visibility","visible");
        }, function(){
            $(this).parent().find("a").css("visibility","hidden");
        }

    );

});// end of ready

Assign the event to your LI's instead 将事件分配给您的LI , and use .mouseenter() and .mouseleave instead of hover() . ,并使用.mouseenter().mouseleave代替hover() This way, as long as your cursor is on the element, or any of its children, the event is still active. 这样,只要您的光标位于元素或其任何子元素上,该事件仍处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM