Skip to content

Instantly share code, notes, and snippets.

@sarveshshejwadkar
Last active November 26, 2016 15:49
Show Gist options
  • Save sarveshshejwadkar/2ba4bfdbb152bc35bcecdf26b059eaa6 to your computer and use it in GitHub Desktop.
Save sarveshshejwadkar/2ba4bfdbb152bc35bcecdf26b059eaa6 to your computer and use it in GitHub Desktop.
Scrolls to Text on Page without using Hash Link
jQuery(document).ready(function ($) {
function scrollBySubNav() {
// when link goes to different page, code keeps searching for text of clicked link
var tmr = setInterval( function () {
var foundElement = $('body').find('h1, h2, h3, h4, h5, h6').filter(':contains("' + Cookies.get('clicked-link') + '")');
if ( foundElement.length ) {
clearInterval(tmr);
$('.site-inner').animate({
scrollTop: foundElement.offset().top - 20
}, 1000);
Cookies.remove('clicked-link');
}
}, 500);
}
scrollBySubNav();
$('.primary-menu a').click( function (e) {
if ( window.location.href.match( $(this).attr('href')) ) {
//does not reload page if clicked link is same as current page
e.preventDefault();
scrollBySubNav();
}
Cookies.set('clicked-link', $(this).html());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment