Cem Cem - 2 months ago 18
HTML Question

Automatically scroll to closest anchor point when between

I was looking for a script that automatically scrolls to the closest anchor point e.g. when between pages.

Just like on this website http://cihadturhan.com/

Answer

I hacked together a fiddle which behaves like the page you mentioned.

I used the following snippet to check when a scroll event ended:

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

Then I compared the offsets from the sections with the window offset (in order to find the closest).

Here is the demo:

https://jsfiddle.net/cz8gz8cd/

(The code could need some improvement)