6

am having a scenario like this below:

In my ui, I will have a textbox. If I have enter a number in the textbox,I need to scroll down to the respective page number.

In Dom, I will have some divs with the respective id's. If user entered a page number as 5. I will check for 5th div offset in dom and get top value. By using scrollTop It will scrolled to the 5th div.

Here, Issue is after scrolled down to the 5th div. If again, entered a page number as 2. offset top value in negative. Hence,ScrollTop defaultly moved to top.

Here is fiddle

To reproduce this exactly,Go to page number 7 and again go to page number 3 or 4.

3
  • Why wouldn't it be negative? When overflow scroll set on container and you are at bottom the top elements are very negative. You are getting that value before the content moves back up
    – charlietfl
    Dec 24, 2014 at 6:20
  • Using position() helps too as you have about 80px of controls above container
    – charlietfl
    Dec 24, 2014 at 6:26
  • You are printing the offset value relative to the current position you are at when you hit enter. Therefore if page X is not in the visible area (is scrolled past), it will print a negative number.
    – ryanlutgen
    Dec 24, 2014 at 6:28

2 Answers 2

11

Late answer, in case it will help someone:

Just use dom offsetTop property. That will be a correct position despite whatever scrolling has happened before.

$( something )[0].offsetTop; 
1
5

http://jsfiddle.net/Ldfmvwr9/3/

Try it

topValue=$('#container').scrollTop() + $("#"+parseInt(userPageNum)).offset().top - $("#"+parseInt(userPageNum)).height() / 2;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.