Skip to content

Instantly share code, notes, and snippets.

@celticwebdesign
Last active November 4, 2018 19:19
Show Gist options
  • Save celticwebdesign/5c02eb32251e44fe4ef63caf67f7be91 to your computer and use it in GitHub Desktop.
Save celticwebdesign/5c02eb32251e44fe4ef63caf67f7be91 to your computer and use it in GitHub Desktop.
Select all links with hashes, MMenu mobile plugin, click menu button and scroll down on mobile.
// Select all links with hashes
$('.menu-item-1385 a, .menu-item-1386 a, .menu-item-1387 a, .menu-item-1383 a')
.click(
function(event) {
// On-page links
// console.log(this);
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
) {
// Figure out element to scroll to
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
// console.log(target);
// Does a scroll target exist?
if (target.length) {
// Only prevent default if animation is actually gonna happen
event.preventDefault();
$('html, body').animate({
// scrollTop: target.offset().top-100
scrollTop: target.offset().top
}, 1000, function() {
// Callback after animation
// Must change focus!
var $target = $(target);
$target.focus();
if ($target.is(":focus")) { // Checking if the target was focused
return false;
} else {
$target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
$target.focus(); // Set focus again
};
});
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment