I'm adding a feature to a WordPress page and I'm stumped.
This page: http://www.cherishresearch.org/about/team/
has four sections, each with an ID (#ca
, #cp
, etc.). I would like a URL entered into the browser to load the page directly at the appropriate section, so http://www.cherishresearch.org/about/team/#cp loads with the Pilot Grant Recipients section at the top of the page.
This should work automatically, but the page ends up loading at the correct position for about a second, then jumps up to a random spot at the top of the page.
I've tried writing some Javascript in an attempt to correct the issue, and it works locally, but not on the live site.
Each section set up like this:
<div class="team-breadcrumb" id="cp">
<h3>Pilot Grant Recipients</h3>
<div class="row">
<!-- Content -->
Here is my javascript:
var hash = window.location.hash.substring(1);
if (hash == 'ci') {
var top = document.getElementById('ci').offsetTop;
window.scrollTo(0, top);
}
if (hash == 'cs') {
var top = document.getElementById('cs').offsetTop;
window.scrollTo(0, top);
}
if (hash == 'cp') {
var top = document.getElementById('cp').offsetTop;
window.scrollTo(0, top);
}
if (hash == 'ca') {
var top = document.getElementById('ca').offsetTop;
window.scrollTo(0, top);
}
I have tried this within both $(document).ready();
and $(window).load();
I've tried window.location.assign()
using the full URL with anchor link. I have no idea what's malfunctioning. Any thoughts?