0

I am trying to animate a div on a link click to 3px at bottom if the position is already 30%, and goes up if it's 3px.

$(document).ready(function () {

    $('#picsClick').click(function () {

        if ($('#mainLogo').position().bottom === '30%') {
            $("#mainMenu").animate({ bottom: "3px" }, 1100);
        }
        else if (($('#mainLogo').position().bottom === '3px')) {
            $("#mainMenu").animate({ bottom: "30%" }, 1100);

        }

    });
});

Somehow it does not run as I have set mainMenu div position to bottom 30% and it's position is absolute.
Thanks.

t.niese
  • 39,256
  • 9
  • 74
  • 101
designerNProgrammer
  • 2,621
  • 5
  • 34
  • 46

2 Answers2

1

The object returned by position() only contains the properties left and top. So bottom is and unexisting property and therefor your code fails.

For more information take a look at the jQuery documentation.

Wim Mostmans
  • 3,485
  • 1
  • 20
  • 20
0

The jquery position API does gives an element's coordinates. X and Y - which for you would mean top and left. You would need to refactor your logic around these. Even after you do that, you should note that the returned values are numbers - absolute values based on your elements position w.r.t page's dimensions. You cannot expect percentages. Hence your comparison logic too would change.

For example, if this were your element:

<input type="text" id="inpId" name="address" value="* Property Address :" style="top: 30%; position: absolute; ">
console.log(typeof $('#inpId').position()['top'])  // "number"
console.log(typeof $('#inpId').position()['bottom'])  // "undefined"

See jquery position

EDIT: to center your logic around bottom, you need to calculate the same in terms of top. See this question:

Get bottom and right position of an element

Hope it helps :)

Community
  • 1
  • 1
Rajat Sharma
  • 502
  • 4
  • 8
  • 17