开发者

Show Div on Hover

I have the following JavaScript (with jQuery)

$(function() {

  $(".btnDesc").click(function() {
    $("#desc").slideToggle("slow");
    $(this).toggleClass("active");
  });

When I change .click(function() to .hover(function(), as expected, hovering over the link with class="bt开发者_运维技巧nDesc" shows the div instead of clicking it, however, I would like to make it show when you stop hovering over the link, the div stays until you click a hide button. With it how I have it, once you stop hovering over the link, the div slides back up.


You need to use mouseover instead of hover, which captures the event of the mouse moving over the link but doesn't bind to when the mouse moves off of the link.


The hover function in jquery requires two functions within it - the mouseover and mouseout functions... e.g.

    $(".btnDesc").hover(
        function() {
            // code to run when mouse hovers in
            $("#desc").show("slow");
            $(this).toggleClass("active");
        },
        function() {
            // code to run when mouse hovers out
            $("#desc").hide("slow");
            $(this).toggleClass("active");
        }
    );

That will show your desc element when you hover over, and hide it when you hover out. The jquery documentation on this function is pretty good too if you get stuck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜