views:

57

answers:

4

Yes, I know the title sounds a little suspicious. I will try to explain this the best I can...

The code below is supposed to have the blue div slide down beside the red div. (Directly to the right - using the position() utility of jQuery UI) The first time you hit the Show the div button, it works. Also, the Hide the div works.

Then when I click to show the div again, it appears to the right of where it is supposed to be! Why is this?!?

Note: You can find a live example of the code here

<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Demo</title>
<style type='text/css'>

#red {
    background-color: red;
    width: 200px;
    height: 150px;
    position: absolute;
}

#blue {
    background-color: blue;
    width: 150px;
    height: 200px;
    position: absolute;
    display: none;
}

#tester_1 {
    top: 300px;
    left: 300px;
    position: absolute;
}

#tester_2 {
    top: 350px;
    left: 300px;
    position: absolute;
}

</style>
</head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui.js"></script>
<script type='text/javascript'>

function Show()
{
    $('#blue').position({
            of: $('#red'),
            my: 'left top',
            at: 'right top'}).slideDown();
}

function Hide()
{
    $('#blue').hide();
}

</script>
<body>
<div id='red'></div>
<div id='blue'></div>
<button id='tester_1' onclick='Show()'>Show the <kbd>div</kbd></button>
<button id='tester_2' onclick='Hide()'>Hide the <kbd>div</kbd></button>
</body>
</html>
A: 

It looks like youre trying to use jQuery UI position, but you did not reference it

http://jqueryui.com/demos/position/

Yisroel
I mentioned jQuery UI in the title...
George Edison
yes, but jQuery UI is not included in your demo.
Yisroel
Ooooooh yeah. No, the reason for that is I copy-and-pasted the small 4kb of jQuery UI that provides `position()` and stuck it to the end of `jquery.js`. I can see where the confusion is though...
George Edison
yeah, i see it now. well, then thats not the issue.
Yisroel
+2  A: 
function Show()
{
    $('#blue').position({
            of: $('#red'),
            my: 'left top',
            at: 'right top'})
    .slideDown();
}

I can't figured out what's with the parameters of your position.
Try to cut that to

   function Show()
    {
        $('#blue').slideDown();
    }

and it will work.

EDIT

based on additional comments try this

$(function(){
   $('#blue').position({
            of: $('#red'),
            my: 'left top',
            at: 'right top'})
})
function Show()
{
    $('#blue').slideDown();
}

function Hide()
{
    $('#blue').hide();
}
Reigel
Sorry, I forgot to mention that the whole point of the thing is that the blue `div` is supposed to be right beside the red `div`. This sample uses jQuery UI.
George Edison
see edit please...
Reigel
There. That seems to work. Still, it is a strange bug.
George Edison
it's not a bug... its just you're calling `.position()` in every clicks... that's why... and `.position()` initializes based on it's current position....
Reigel
+2  A: 

Try just resetting the blue div... I did this and it appears to work on Chrome and IE now.

function Hide()
{
    $('#blue').css({ left: 0, top: 0 }).hide();
}
fudgey
A: 

Any measurement you make (directly or via jQuery UI) when the element is hidden (using display:none) is going to yield 0. This can lead to funny results if you assume the element is visible.

In your example, you are positioning the element when it is not in the display list and then doing slideDown() and that's what is causing the problem.

Chetan Sastry