views:

285

answers:

4

How does stackoverflow does that hovering effect on a tag of a question? How to do the same using jquery?

EDIT: Not that mouseover i want the submenu showing Add Jquery to favorite tags

A: 

For a simple hover effect you really should use css, like

a.hover:hover {
   background-color: #ff0000;
}

for instance.

If it must be in jQuery, it would look like

$('a.myanchorclass').hover(function(){
     $(this).css('background-color', '#ff0000');
}, function(){
     $(this).css('background-color', '#000000');
});
jAndy
@jandy look at me edit..
MuraliVijay CSK
@jandy please help me out..
MuraliVijay CSK
I can't see why this is downvoted, seems like a perfectly valid solution?
Sander Rijken
A: 

I agree with both just to emphasize, you absolutely can use the :hover in CSS, if you are targeting browsers newer then IE6, IE6 won't work with :hover unless it's a link.

Avi Tzurel
+12  A: 

This should do the work.

http://jsfiddle.net/5GD6r/4/

I have coded it almost exactly the way stackoverflow does. I built upon the button already created by @Reigel.

Hope this helps. Cheers :)

EDIT: Added the answer here too from jsFiddle on request.

HTML:

<a href="#" class="post-tag" title="show questions tagged 'mousehover'" rel="tag">Ruby-on-Rails</a>

<a href="#" class="post-tag" title="show questions tagged 'mousehover'" rel="tag">JQuery</a>

<a href="#" class="post-tag" title="show questions tagged 'mousehover'" rel="tag">CSS</a>


<div class="mouseoverHoverBox">
    <span class="pointer">
        <span class="plusminus">+</span>
        <span class="addRemove">Add </span>
        <span class="insert"></span>
        <span class="fromTo"> To </span>
        <span>Interesting tags</span>
    </span>
    <br />
    <span class="pointer">
        <span class="plusminus">+</span>
        <span class="addRemove">Add</span>
        <span class="insert"></span>
        <span class="fromTo"> To </span>
        <span>Ignored tags</span>
    </span>
</div>

CSS:

.post-tag {
    position:relative;
    background-color: #E0EAF1;
    border-bottom: 1px solid #3E6D8E;
    border-right: 1px solid #7F9FB6;
    color: #3E6D8E;
    font-size: 90%;
    line-height: 2.4;
    margin: 2px 0px 2px 0px;
    padding: 3px 4px;
    text-decoration: none;
    white-space: nowrap;
    }

.post-tag:hover {
    position:relative;
    background-color:#3E6D8E;
    color:#E0EAF1;
    border-bottom:1px solid #37607D;
    border-right:1px solid #37607D;
    text-decoration:none;
}

.mouseoverHoverBox {
    position:relative;
    top: -6px;
    border: 2px ridge #CCC;
    padding: 10px;
    width: 250px;
}

.plusminus {
    color: #E45C0E;
}

.pointer {
    cursor: pointer;
    width: 100%;
    height: 100%;
    color: #3E6D8E;
}

JAVASCRIPT:

$('.mouseoverHoverBox').hide();

$('.post-tag').live('mouseover',function(e){
    var x = $(this).offset();
    $('.mouseoverHoverBox').css('left',x.left-10);
    $('.insert').html(' <b>'+$(this).text() + '</b> ');
    $('.mouseoverHoverBox').slideDown();
});

$('.pointer').live('mouseover mouseout', function(e){
    if(e.type=="mouseover") {
        $(this).css('text-decoration','underline');
    }else if(e.type="mouseout") {
        $(this).css('text-decoration','none');
    }
});

$('.pointer').toggle(function() {
   $(this).find('.plusminus').text('x ');
   $(this).find('.addRemove').text('Remove ');
   $(this).find('.fromTo').text('From');
}, function() {
   $(this).find('.plusminus').text('+ ');
   $(this).find('.addRemove').text('Add ');
   $(this).find('.fromTo').text('To');
});

$('.mouseoverHoverBox').mouseleave(function(){
     $(this).hide();
});
Shripad K
@Shripad K: Thanks. +1
Brock Adams
A: 

There are many jQuery tooltip plug-ins available. You could try using one of them.

kgiannakakis