[英]jQuery: How can I detect offset that page will return on refresh?
如果立即使用$(“ body”)。offset(),則結果始終為0,即使該網址包含錨點也是如此。
有沒有更好的方法來獲取頁面實際解析位置的偏移量?
謝謝!
那是因為$("body").offset()
返回正文相對於頁面的頂部和左側值。
您可能要使用
$(window).scrollTop();
如果需要滾動位置的值。
//this will alert the scroll pos on load
$(document).ready(function(){
var scrollPos = $(window).scrollTop();
alert(scrollPos);
});
- 演示3: http : //jsbin.com/aroha5/3
$.fn.extend({
scrollTo : function(speed, easing) {
return this.each(function() {
var targetOffset = $(this).offset().top;
$('html,body').animate({scrollTop: targetOffset}, speed, easing);
});
}
});
$('#scroll-to').scrollTo(1000);
- 演示: http : //jsbin.com/aroha5
- 演示2: http : //jsbin.com/aroha5/2
/* body offset start */ $('body').offset().top
/* body offset end */ $('body').height()
但是$("body").offset()
返回的Object
不是0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.