jQuery偏移量返回负值 [英] jQuery Offset returns negative value
问题描述
我遇到的情况如下:
在我的用户界面中,我将有一个文本框.如果我在文本框中输入了数字,则需要向下滚动到相应的页码.
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.
在Dom
中,我将有一些带有各自ID的div.如果用户输入的页码为5,我将检查dom中的第5个div偏移量并获得最高价值.通过使用scrollTop,它将滚动到第5格.
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.
在这里,问题向下滚动到第5格.如果再次输入页码,则输入2.作为负的偏移最高值.因此,ScrollTop默认移至顶部.
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.
要完全重现此内容,请转到第7页,然后再次转到第3或4页.
To reproduce this exactly,Go to page number 7 and again go to page number 3 or 4.
推荐答案
http://jsfiddle.net/Ldfmvwr9 /3/
尝试
topValue=$('#container').scrollTop() + $("#"+parseInt(userPageNum)).offset().top - $("#"+parseInt(userPageNum)).height() / 2;
这篇关于jQuery偏移量返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!