之间自动滚动到最近的锚点

Automatically scroll to closest anchor point when between

我正在寻找一个自动滚动到最近锚点的脚本,例如在页面之间。

就像在这个网站上一样http://cihadturhan.com/

我一起破解了一个 fiddle,它的行为类似于您提到的页面。

我使用以下代码片段来检查滚动事件何时结束:

$.fn.scrollStopped = function(callback) {
  var that = this
  var $this = $(that);
  $this.scroll(function(ev) {
    clearTimeout($this.data('scrollTimeout'));
    $this.data('scrollTimeout', setTimeout(callback.bind(that), 250, ev));
  });
};

然后我将这些部分的偏移量与 window 偏移量进行比较(以便找到最接近的)。

这是演示:

https://jsfiddle.net/cz8gz8cd/

(代码可能需要一些改进)