Skip to content

Instantly share code, notes, and snippets.

@Pavracer
Created February 17, 2025 01:21
Show Gist options
  • Save Pavracer/f44b949caf99ef02abe9068db28c8bb3 to your computer and use it in GitHub Desktop.
Save Pavracer/f44b949caf99ef02abe9068db28c8bb3 to your computer and use it in GitHub Desktop.
ChatGPT
The issue with your script likely stems from **overriding** Divi’s built-in smooth scrolling function (`window.et_pb_smooth_scroll`) in a way that breaks Dot Navigation. Here’s why:
### Problems in Your Script:
1. **Scope Limitation (`if($window_width <= 500)`)**
- Your function **only executes** when the window width is **500px or less**.
- This means that for larger screens, **Divi’s default smooth scrolling is entirely disabled**, breaking Dot Navigation.
2. **Missing `return` Statement**
- Your function doesn’t return any value, which could cause unexpected behavior if Divi expects a return from `et_pb_smooth_scroll`.
3. **Modifying Global Function After a Delay (`setTimeout`)**
- You're redefining `window.et_pb_smooth_scroll` **after 200ms**, which may not align with how Divi initializes navigation.
- If Dot Navigation tries to call `et_pb_smooth_scroll` before your script modifies it, the default behavior may already be disrupted.
### Possible Fix:
Instead of fully overriding `window.et_pb_smooth_scroll`, extend it properly to **keep Divi’s default behavior** while adding your mobile-specific offset.
Try this adjusted script:
```javascript
<script>
jQuery(function($) {
setTimeout(function() {
var originalSmoothScroll = window.et_pb_smooth_scroll;
window.et_pb_smooth_scroll = function($target, $top_section, speed, easing) {
var $window_width = $(window).width();
var $menu_offset = 0;
var headerHeight = 117;
if ($window_width <= 500) {
headerHeight = 131;
}
if ($('#wpadminbar').length && $window_width <= 980) {
$menu_offset += $('#wpadminbar').outerHeight() + headerHeight;
} else {
$menu_offset += headerHeight;
}
// Fix sidenav scroll to top
var $scroll_position = $top_section ? 0 : $target.offset().top - $menu_offset;
// Use original smooth scroll for wider screens
if ($window_width > 500) {
return originalSmoothScroll($target, $top_section, speed, easing);
}
// Mobile-specific smooth scrolling
speed = 1000;
if (typeof easing === 'undefined') {
easing = 'swing';
}
$('html, body').animate({ scrollTop: $scroll_position }, speed, easing);
};
}, 200);
});
</script>
```
### Why This Works:
✅ **Preserves Divi’s default smooth scroll behavior on wider screens**
✅ **Ensures Dot Navigation remains functional**
✅ **Applies your custom offset logic only for mobile (`<= 500px`)**
✅ **Keeps the return behavior intact**
Let me know if you still experience issues after implementing this fix! 🚀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment