Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a div next to the hovered element with jQuery?

Let's say i have several div's like these:

EDIT:

<div class="ProfilePic">
    <a href="#">
        <img src="lib/css/img/profile_pic1.png" alt="" class="ProfilePicImg"/>
    </a>

    <div class="PopupBox" style="display:none;"> ... </div>
</div>

I want to be able to hover over the image .ProfilePicImg and show a another div relatively to it.

The box to popup on hover is set to position:absolute. And the .ProfilePic's position is relative. Just like it should be.

I have tried different solutions, but in vain... And I have also searched around here on StackOverflow...

Does anyone have a trick for this?

P.S. I don't want the popup box to show on each of the .ProfilePic div's I have...

EDIT2: It seems jQuery's .find() traversal function was the key to fetch the specific .PopupBox i wanted to show, instead of the all.

like image 780
KristianB Avatar asked May 10 '11 15:05

KristianB


People also ask

How to make a hidden div in HTML?

To hide an element, set the style display property to “none”. document. getElementById("element"). style.

How to show and hide div using CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

Is hover () a jQuery event method?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element.


1 Answers

I haven't tested the code but is this what you are looking for?

$(".ProfilePicImg").hover(function(){
    var divToShow = $(this).parent("a").siblings("div.PopupBox");
    divToShow.css({
        display: "block",
        position: "absolute",
        left: ($(this).offset().left + $(this).width()) + "px",
        top: $(this).offset().top + "px"
    });
},
function(){
    $("div.PopupBox").hide();   
});
like image 51
Sang Suantak Avatar answered Nov 10 '22 00:11

Sang Suantak