开发者

jQuery: How can I detect offset that page will return on refresh?

If you use $("body").offset() onread开发者_JS百科y the result is always 0, even if the url includes an anchor.

Is there a better way to get the offset of where the page will actually resolve to?

Thanks!


Thats because $("body").offset() returns the top and left values of the body relative to the page.

You probably want to use

$(window).scrollTop();

if you want the value of the scroll position.

//this will alert the scroll pos on load
$(document).ready(function(){

    var scrollPos = $(window).scrollTop();
    alert(scrollPos);

});


  • DEMO 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);

  • DEMO: http://jsbin.com/aroha5
  • DEMO2: http://jsbin.com/aroha5/2
/* body offset start */ $('body').offset().top 
/* body offset end */  $('body').height() 

however $("body").offset() return an Object not 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜