This script smooths anchor navigation with Lenis, turning abrupt jumps into fluid, controlled scroll transitions that feel deliberate and refined.
Copy & paste the scripts before the </body> tag of your project. If you added them before for another setup, skip this step.
Right-click in Elementor, choose “Paste from another site,” and while the popup is open, press cmd/ctrl + v to insert the layout.
Place the code in an HTML widget or add it through Elementor → Custom Code (before the closing </body> tag) either globally or only on selected pages.
Paste the code through the page or site settings, or add it via Elementor → Custom Code (before </body>) for broader use.
Paste the script through Elementor → Custom Code (set to load after </body>) for site-wide or page-specific loading.
document.addEventListener("DOMContentLoaded", () => {
if (document.body.classList.contains("elementor-editor-active")) return;
const lenis = window.ouLenis;
if (!lenis) return;
// Anchor scroll duration
const anchorDuration = 1.2;
// Anchor offset from top
const anchorOffset = 0;
document.addEventListener("click", e => {
const link = e.target.closest('a[href^="#"]');
if (!link) return;
const href = link.getAttribute("href");
if (href === "#" || !href.startsWith("#")) return;
const target = document.querySelector(href);
if (!target) return;
e.preventDefault();
lenis.scrollTo(target, {
duration: anchorDuration,
offset: anchorOffset,
easing: t => 1 - Math.pow(1 - t, 3)
});
});
});Place the PHP snippet inside your theme’s functions.php or using any code snippet to enable logic.
These options control how anchor scrolling behaves.
The anchorDuration value is a number in seconds (for example, 1.2) and defines how long the anchor scroll animation takes, with higher values creating slower, more cinematic movement and lower values keeping it quick and direct.
The anchorOffset value is a number in pixels (for example, 80) and controls how far from the top the scroll stops when navigating to an anchor, which is useful when a sticky header might otherwise overlap the target section.