Skip to content

Instantly share code, notes, and snippets.

@ankedsgn
Last active December 28, 2021 12:45
Show Gist options
  • Save ankedsgn/ea0d453d14ad356b3b6a86d987c01c92 to your computer and use it in GitHub Desktop.
Save ankedsgn/ea0d453d14ad356b3b6a86d987c01c92 to your computer and use it in GitHub Desktop.
$(function() {
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $id.offset().top;
// animated top scrolling
$('body, html').animate({scrollTop: pos});
});
});
//for drupal:
Drupal.behaviors.smoothHrefScroll = {
attach: function (context) {
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $id.offset().top;
// animated top scrolling
$('body, html').animate({scrollTop: pos});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment