title: Change url when manually scrolled to an id even scrolling to top?
body: <p>Is it possible to change the URL in the address bar instantly when I scroll to an id? Or have a long document with multiple id and the url changes on address bar, when I hit a new id even i scroll form bottom to top.</p>
  
  <p>i use <a href="https://stackoverflow.com/questions/14660580/change-url-when-manually-scrolled-to-an-anchor">Change url when manually scrolled to an anchor?</a></p>
  
  <p>but this is not working when scroll from bottom to top.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function () {
    var currentHash = "#";
    $(document).scroll(function () {
      $('.block').each(function () {
        var top = window.pageYOffset;
        var distance = top - $(this).offset().top;
        var hash = $(this).attr('id');
        // 30 is an arbitrary padding choice, 
        // if you want a precise check then use distance===0
        if (distance &lt; 30 &amp;&amp; distance &gt; -30 &amp;&amp; currentHash != hash) {
          window.location.hash = (hash);
          currentHash = hash;
        }
      });
    });
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    margin: 0px;
  }
  div {
    height: 900px;
    text-align: center;
    padding: 15px;
    background-color: #00f;
  }
  div:nth-child(even) { background: #ccc; }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;div class="block" id="one"&gt;Block 1&lt;/div&gt;
  &lt;div class="block" id="two"&gt;Block 2&lt;/div&gt;
  &lt;div class="block" id="three"&gt;Block 3&lt;/div&gt;
  &lt;div class="block" id="four"&gt;Block 4&lt;/div&gt;
  &lt;div class="block" id="five"&gt;Block 5&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks in advance.</p>