I was executing a provided design that used one long page with menu links to various anchors in the content instead of multiple individual pages.
I was banging my head against the wall trying to figure out why the menu links did not stop at the correct anchor id when clicked on Macs and iOS. Then I read this:
When building layouts with Divi on WordPress, a known discrepancy occurs in Webkit browsers on macOS/iOS (such as Safari and Chrome for Mac). When the Divi builder or custom scripts invoke jQuery’s .outerHeight(), hidden elements (display: none or elements hidden via Divi’s responsive visibility settings) incorrectly return a positive integer instead of 0.
This happens because Webkit handles the layout tree computation differently for elements wrapped in parent containers that are dynamically manipulated, causing jQuery to grab a cached or subpixel layout dimension before the display: none state is properly computed.
Turns out this is my issue. Webkit browsers on Mac are adding the height of a hidden form (position: absolute, display:none) and jumping below the anchor point exactly the same number of pixels as the form height.
Has anyone else experienced this? If so, what was your solution/workaround?
For me, overriding the default behavior seemed to fix it.
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
setTimeout(() => {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}, 50);
console.log("scroll to "+targetId);
}
});
});