简体   繁体   中英

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

If you use $("body").offset() onready 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);

});
$.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);

/* body offset start */ $('body').offset().top 
/* body offset end */  $('body').height() 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM