Search code examples
jqueryhoverappendto

Clearing .appendTo when element isn't being hovered over


Alright so I used .appendTo to beat an issue of overflow:hidden so my entire tooltip would show outside the containing div. However, it locks the tooltip in place after the item was hovered over. How do I go about clearing the .appendTo to hide the tooltip.

$(this).hover(function(){
var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height:        this.offsetHeight});
$(this).children('.browse-tip').css({top: -40, left: pos.left - pos.width / 2});
$(this).children('.browse-tip').show();
$(this).children('.browse-tip').appendTo('#browse_wrap');
},function() {
$(this).children('.browse-tip').hide();
});

Solution

  • The whole structure of the thing is making your work challenging. I recommend having an independent element that you can float around the page toggling when needed, and just putting the relevant info into it before showing:

    <div id="popup"></div>
    
    <style>
    #popup {
        position: absolute;
        display: none;
        z-index: 999999;
        width: 220px;
        height: 200px;
    }
    
    #popup .browse-tip {
        display: block;
    }
    </style>
    
    <script>
    $(this).hoverIntent(function(){
        var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
        $(this).children('.browse-tip').clone().appendTo("#popup");
        $("#popup").css({top: pos.top-40, left: pos.left - pos.width / 2});
        $("#popup").show();
        return true;
    },function() {
        $("#popup").hide().children().remove();
        return false;
    });
    </script>
    

    http://jsfiddle.net/pMSRp/