Cem Cem - 4 months ago 38
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/


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) {
    $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:


(The code could need some improvement)