Encountering an issue with a markdownify plugin led me to search for a solution regarding the link's target. After some troubleshooting, I found a workaround that proved effective for various element selectors. Specifically, my link appeared as
<a href="#target">some text</a>
, while the actual target needed to be
<a href="target"></a>
. To address this scenario, the following function can be implemented:
var scrollAnchorSamePage = function() {
$('a[href^="#"]').click(function() {
event.preventDefault();
var id = $(this).attr("href");
// Removing the hashtag at the beginning:
var target = $('a[href^="' + id.substring(1, id.length) + '"]');
// Including an offset adjustment for a sticky header (140 pixels)
$('html, body').animate({ scrollTop: target.offset().top - (160) }, 1000);
});
}
scrollAnchorSamePage();
To ensure the proper functionality, ensure the inclusion of the necessary jQuery packages prior to the closing </body>
tag in your document:
<!-- Latest minified jQuery -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>