Advertisement
michaellevelup

Add scroll to custom button

Nov 10th, 2022
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. add_action('wp_footer', function(){
  2.  
  3. ?>
  4.  
  5. <script>
  6. document.addEventListener("DOMContentLoaded", function() {
  7. const comfort = document.querySelector('a[href="#comfort"]'),
  8.         services = document.querySelector('a[href="#services"]'),
  9.         community = document.querySelector('a[href="#community"]');
  10.        
  11.  
  12. function tabScrollTo( btn, div ){
  13.     btn.addEventListener('click', function(e){
  14.     e.preventDefault();
  15.    
  16.     setTimeout(function() {
  17.         var t = Math.floor( document.getElementById(div).getBoundingClientRect().top )
  18.     console.log(t);
  19.     window.scrollBy({ top: t, left: 0, behavior: 'smooth' });
  20.     }, 10);
  21.    
  22. });
  23. }
  24.  
  25. if ( comfort || services || community ) {
  26.     tabScrollTo( comfort, 'kt-layout-id_f5461f-b2' );
  27.     tabScrollTo( services, 'kt-layout-id_239a8b-04' );
  28.     tabScrollTo( community, 'kt-layout-id_e3ab64-4a' );
  29. }
  30. });
  31. </script>
  32.  
  33. <?php
  34.  
  35. }, 99);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement