Get vertical position of scrollbar for a webpage on pageload when the url contains an anchor

好久不见. 提交于 2019-12-05 21:46:51

You might just want to do something quick-and-dirty like setTimeout for however many milliseconds it takes to get it from Chrome or Safari reliably

var MAX_CHECKS = 5;             // adjust these values
var WAIT_IN_MILLISECONDS = 100; // however works best
var checks = 0;

function checkScroll() {
  if ($(this).scrollTop() > 0) {
    // then a conditional statement based on the scrollTop() value
    if ($(this).scrollTop() > $("#sidenav").height()) {
      ...
    }
  } else {
    if (++checks < MAX_CHECKS) {
      // just to make sure, you can try again
      setTimeout(checkScroll, WAIT_IN_MILLISECONDS);
    }
  }
}

$(document).ready(function() {
  // You can also throw in an if statement here to exclude
  // URLs without # signs, single out WebKit browsers, etc.
  setTimeout(checkScroll, WAIT_IN_MILLISECONDS);
  ...
});

Note that you can also use if ($.browser.webkit), although this feature is deprecated and may be moved to a plugin instead of the main jQuery (in favor of feature detection).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!