Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

jquery onclick anchor tag scroll to div with exact position

function offsetAnchor() {
  if (location.hash.length !== 0) {
    window.scrollTo(window.scrollX, window.scrollY - 30);
  }
}
// call this function after event fire offsetAnchor() //
$(document).ready(function(){
    $("a").on('click', function(event) {
       window.location.hash = '';
        if (this.hash !== "") {
            event.preventDefault();
            var hash = this.hash;
            $('html, body').animate({
                scrollTop: $(hash).offset().top
            }, 800, function(){
                window.location.hash = hash;
                offsetAnchor();
            });
        }
    });
});
 
PREVIOUS NEXT
Tagged: #jquery #onclick #anchor #tag #scroll #div #exact #position
ADD COMMENT
Topic
Name
1+8 =