Luke
Luke

Reputation: 21236

jQuery mobile pageshow getting element position

In my jQuery Mobile app I want to "slide" the UI to a certain position when a page is displayed but it seems that when I get the offset object of the element on the page I want to slide to in the pageshow event, that the y coordinate is always 1.

My code looks something like this:

$(document).on('pageshow', function() {
   var offset = $('#my-element').offset();
   // offset.top is always 1
} );

What would be the best way to overcome this issue?

Upvotes: 0

Views: 510

Answers (1)

Gajotres
Gajotres

Reputation: 57309

This is a wild guess but I know only one case where this can happen.

It can happen if element you are searching for also exist in some other page, mainly if it will be find in the DOM before element you are currently searching. Because that page is not visible it don't have height.

This should help, change

var offset = $('#my-element').offset();

to this:

var offset = $.mobile.activePage.find('#my-element').offset();

$.mobile.activePage is a selector that will give you access to currently active page. It will disregard everything else.

Upvotes: 2

Related Questions