Advertisement
brooklyndesignstudio

Fix Anchor Link Scroll Position

Jun 30th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- Fix Anchor Link Scroll Position -->
  2. <script>
  3. document.addEventListener('DOMContentLoaded', function(event){
  4.  
  5.     if (window.location.hash) {
  6.         // Start at top of page
  7.         window.scrollTo(0, 0);
  8.        
  9.         // Prevent default scroll to anchor by hiding the target element
  10.         var db_hash_elem = document.getElementById(window.location.hash.substring(1));
  11.         window.db_location_hash_style = db_hash_elem.style.display;
  12.         db_hash_elem.style.display = 'none';
  13.        
  14.         // After a short delay, display the element and scroll to it
  15.         jQuery(function($){
  16.             setTimeout(function(){
  17.                 $(window.location.hash).css('display', window.db_location_hash_style);
  18.                 et_pb_smooth_scroll($(window.location.hash), false, 300);
  19.             }, 200);
  20.         });    
  21.     }
  22. });
  23. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement