AI Verified

Name

Elementor Anchor Link Scroll Offset(PHP)

About

Offset anchor menu links from the top of the page.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

Set desktop, tablet, and mobile values to desired offset in PX

Codevault

Robert-Wattner's-Snippets

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

16/03/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Elementor Anchor Link Scroll Offset(PHP)

 
                    
1// Function to enqueue JavaScript code snippet
2function enqueue_custom_js_script() {
3 // Define scroll top distances for different viewport widths
4 $desktop = 120; // Scroll top distance for desktop (viewport width >= 1025px)
5 $tablet = 100; // Scroll top distance for tablet (viewport width between 768px and 1024px)
6 $mobile = 80; // Scroll top distance for mobile (viewport width below 768px)
7 ?>
8 <script>
9 window.addEventListener('elementor/frontend/init', function() {
10 elementorFrontend.hooks.addFilter( 'frontend/handlers/menu_anchor/scroll_top_distance', function(scrollTop) {
11 // Get viewport width
12 var viewportWidth = window.innerWidth;
13 
14 // Define scroll top distance based on viewport width
15 if (viewportWidth >= 1025) {
16 // Desktop
17 return scrollTop - <?php echo $desktop; ?>;
18 } else if (viewportWidth >= 768 && viewportWidth <= 1024) {
19 // Tablet
20 return scrollTop - <?php echo $tablet; ?>;
21 } else {
22 // Mobile
23 return scrollTop - <?php echo $mobile; ?>;
24 }
25 });
26 });
27 </script>
28 <?php
29}
30 
31// Hook to enqueue the JavaScript code snippet
32add_action('wp_enqueue_scripts', 'enqueue_custom_js_script');
33?>

0

Related Snippets

Please see some snippets below related to this snippet..

Elementor

AI Verified

0

Elementor Failed Login

Added: 7 months ago

Last Updated: 7 months ago

Return to the current page and add a variable to the URL after a failed login using the Elementor login widget

Elementor

AI Verified

0

Add Media Library to Elementor Finder

Added: 2 years ago

Last Updated: 1 year ago

Add the Media Library to the Elementor Finder

Elementor

AI Verified

0

Enable webp upload

Added: 10 months ago

Last Updated: 10 months ago

Enable drag and drop of webp images into the media library

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Elementor

AI Verified

0

Elementor Anchor Link Scroll Offset(PHP)

Added: 6 months ago

Last Updated: 6 months ago

Offset anchor menu links from the top of the page.

Elementor

Unverified

0

Elementor Anchor Link Scroll Offset

Added: 6 months ago

Last Updated: 6 months ago

Offset anchor menu links from the top of the page.

Elementor

AI Verified

0

Elementor Container Global Gutter Settings

Added: 6 months ago

Last Updated: 6 months ago

Adds custom global gutters to elementor containers.