functions.php (paste at the bottom)
If functions.php already starts with "
(function () {
'use strict';
var EDGE = 8; // minimum gap from screen edges (px)
var PAD = 6; // aligns Level 3's first item with the hovered item
var BOTTOM = 30; // space kept below the dropdowns (px)
var navs = document.querySelectorAll('.navigation-main');
if (!navs.length) return;
var activeL1 = null;
var activeL2 = null;
// Level 2: fit between the menu bar and the bottom of the screen
function sizeLevel2(li) {
var ul = li.querySelector(':scope > ul');
if (!ul) return;
var max = window.innerHeight - li.getBoundingClientRect().bottom - BOTTOM;
if (max < 150) max = 150;
ul.style.setProperty('max-height', Math.round(max) + 'px', 'important');
ul.style.setProperty('overflow-y', 'auto', 'important');
ul.style.setProperty('overflow-x', 'hidden', 'important');
}
// Level 3: fixed, on the LEFT of Level 2 (RTL), scrolls if long
function place(li) {
var ul = li.querySelector(':scope > ul');
if (!ul) return;
var st = ul.style;
var vh = window.innerHeight;
var vw = document.documentElement.clientWidth;
st.setProperty('transition', 'opacity .2s ease-in, visibility .2s ease-in', 'important');
st.setProperty('position', 'fixed', 'important');
void ul.offsetWidth; // force styles to apply before measuring
st.setProperty('margin', '0', 'important');
st.setProperty('right', 'auto', 'important');
st.setProperty('left', '0px', 'important');
st.setProperty('top', '0px', 'important');
st.setProperty('overflow-y', 'auto', 'important');
st.setProperty('overflow-x', 'hidden', 'important');
st.setProperty('max-height', Math.round(vh * 0.7) + 'px', 'important');
var base = ul.getBoundingClientRect();
var pr = li.parentElement.getBoundingClientRect(); // Level 2 box
var r = li.getBoundingClientRect(); // hovered item
var w = base.width;
var h = base.height;
var x = pr.left - w + 1;
if (x < EDGE) {
if (pr.right + w - 1 <= vw - EDGE) x = pr.right - 1;
else x = EDGE;
}
var y = r.top - PAD;
if (y + h > vh - EDGE) y = vh - EDGE - h;
if (y < EDGE) y = EDGE;
st.setProperty('left', (x - base.left) + 'px', 'important');
st.setProperty('top', (y - base.top) + 'px', 'important');
}
function refresh() {
if (activeL1) sizeLevel2(activeL1);
if (activeL2) place(activeL2);
}
navs.forEach(function (nav) {
function enter(e) {
var t = e.target;
if (!t || !t.closest) return;
var from = e.relatedTarget;
var l1 = t.closest('.menu > li.menu-item-has-children');
if (l1 && !(from && l1.contains(from))) {
activeL1 = l1;
activeL2 = null;
sizeLevel2(l1);
}
var l2 = t.closest('.menu > li > ul > li.menu-item-has-children');
if (l2 && !(from && l2.contains(from))) {
activeL2 = l2;
place(l2);
}
}
nav.addEventListener('mouseover', enter);
nav.addEventListener('focusin', enter);
nav.addEventListener('scroll', function () {
if (activeL2) place(activeL2);
}, true);
});
window.addEventListener('resize', refresh);
window.addEventListener('scroll', refresh, { passive: true });
})();