Search
 
SCRIPT & CODE EXAMPLE
 

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();
            });
        }
    });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: chart.js how to aligns legend in the chart 
Javascript :: chrome console angular scope 
Javascript :: type svg react 
Javascript :: for..of 
Javascript :: mobile number validation in javascript with country code 
Javascript :: how to get last element of array in javascript 
Javascript :: angular socket.io with token header 
Javascript :: mongoose response to object 
Javascript :: javascript fetch get data from promise 
Javascript :: javascript global object 
Javascript :: js contain character 
Javascript :: return an object from an array javascript 
Javascript :: disable button in angular 
Javascript :: innertext of input js 
Javascript :: express post not working 
Javascript :: timeline javascript 
Javascript :: javaScript getDay() Method 
Javascript :: 100vh mobile 
Javascript :: react lazy load suspense 
Javascript :: javascript check if string contains only numbers 
Javascript :: js binary search 
Javascript :: javascript NEGATIVE_INFINITY 
Javascript :: if window width jquery then display a div at scroll 
Javascript :: javascript design patterns pdf 
Javascript :: usecontext hook react 
Javascript :: ipcrenderer main.js 
Javascript :: javascript write to firebase 
Javascript :: js var vs const 
Javascript :: @apify/http-request 
Javascript :: keyup in jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =