Tagged: x
-
AuthorPosts
-
February 6, 2017 at 3:58 am #1359400
adriangregoryParticipantHello
On my site http://www.portsmouthmusicacademy.co.uk/cms I’m using anchor links on some pages but when I click the anchor link the page scrolls too far down and you can’t see the start of the section. I think this is due to having a fixed top bar as it only occurs on desktop view. Is there any way around this? I’ve read several similar cases on the forum but can’t find a definitive answer that works.
Look forward to hearing from you.
Thanks
February 6, 2017 at 4:34 am #1359429
Paul RModeratorHi,
To Fix it, you can add this under Custom > Edit Global JS in the Customizer.
jQuery(document).ready(function($) { $('a[href*="#"]').off('touchend click'); var $body = $('body'); var bodyHeight = $body.outerHeight(); var adminbarHeight = $('#wpadminbar').outerHeight(); var navbarFixedTopHeight = $('.x-navbar-fixed-top-active .x-navbar').outerHeight(); var logobarFixedTopHeight = $('.x-logobar').outerHeight(); var locHref = location.href; var locHashIndex = locHref.indexOf('#'); var locHash = locHref.substr(locHashIndex); var dragging = false; $body.on('touchmove', function() { dragging = true; } ); $body.on('touchstart', function() { dragging = false; } ); // // Calculate the offset height for various elements and remove it from // the element's top offset so that fixed elements don't cover it up. // function animateOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight - logobarFixedTopHeight + 1 }, ms, easing); } // // Page load offset (if necessary). // $(window).load(function() { if ( locHashIndex !== -1 && $(locHash).length ) { animateOffset(locHash, 1, 'linear'); } }); // // Scroll trigger. // $('a[href*="#"]').on('touchend click', function(e) { href = $(this).attr('href'); notComments = href.indexOf('#comments') === -1; if ( href !== '#' && notComments ) { var theId = href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); if (dragging) { return; } animateOffset($el, 850, 'easeInOutExpo'); } } }); // // Initialize scrollspy. // if ( $body.hasClass('x-one-page-navigation-active') ) { $body.scrollspy({ target : '.x-nav-wrap.desktop', offset : adminbarHeight + navbarFixedTopHeight + logobarFixedTopHeight }); // // Refresh scrollspy as needed. // $(window).resize(function() { $body.scrollspy('refresh'); }); var timesRun = 0; var interval = setInterval(function() { timesRun += 1; var newBodyHeight = $body.outerHeight(); if ( newBodyHeight !== bodyHeight ) { $body.scrollspy('refresh'); } if ( timesRun === 10 ) { clearInterval(interval); } }, 500); } });Hope that helps.
February 6, 2017 at 4:51 am #1359444
adriangregoryParticipantAmazing, thank you! The only thing now is that the offset is wrong on mobile/tablet screens where there isn’t a fixed top bar. Is it possible to make this code apply only to desktop screens?
February 6, 2017 at 6:31 am #1359551
Paul RModeratorHi,
Please replace the code with this.
jQuery(document).ready(function($) { $('a[href*="#"]').off('touchend click'); var $body = $('body'); var bodyHeight = $body.outerHeight(); var adminbarHeight = $('#wpadminbar').outerHeight(); var navbarFixedTopHeight = $('.x-navbar-fixed-top-active .x-navbar').outerHeight(); var logobarFixedTopHeight = $('.x-logobar').outerHeight(); var locHref = location.href; var locHashIndex = locHref.indexOf('#'); var locHash = locHref.substr(locHashIndex); var dragging = false; $body.on('touchmove', function() { dragging = true; } ); $body.on('touchstart', function() { dragging = false; } ); // // Calculate the offset height for various elements and remove it from // the element's top offset so that fixed elements don't cover it up. // function animateOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight - logobarFixedTopHeight + 1 }, ms, easing); } function animatemobileOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight + 1 }, ms, easing); } // // Page load offset (if necessary). // $(window).load(function() { if ( locHashIndex !== -1 && $(locHash).length ) { animateOffset(locHash, 1, 'linear'); } }); // // Scroll trigger. // $('.x-nav-wrap.desktop a[href*="#"]').on('touchend click', function(e) { href = $(this).attr('href'); notComments = href.indexOf('#comments') === -1; if ( href !== '#' && notComments ) { var theId = href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); if (dragging) { return; } animateOffset($el, 850, 'easeInOutExpo'); } } }); $('.x-nav-wrap.mobile a[href*="#"]').on('touchend click', function(e) { href = $(this).attr('href'); notComments = href.indexOf('#comments') === -1; if ( href !== '#' && notComments ) { var theId = href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); if (dragging) { return; } animatemobileOffset($el, 850, 'easeInOutExpo'); } } }); // // Initialize scrollspy. // if ( $body.hasClass('x-one-page-navigation-active') ) { $body.scrollspy({ target : '.x-nav-wrap.desktop', offset : adminbarHeight + navbarFixedTopHeight + logobarFixedTopHeight }); // // Refresh scrollspy as needed. // $(window).resize(function() { $body.scrollspy('refresh'); }); var timesRun = 0; var interval = setInterval(function() { timesRun += 1; var newBodyHeight = $body.outerHeight(); if ( newBodyHeight !== bodyHeight ) { $body.scrollspy('refresh'); } if ( timesRun === 10 ) { clearInterval(interval); } }, 500); } });Hope that helps.
February 6, 2017 at 8:14 am #1359654
adriangregoryParticipantYeah that’s great, thank you. How can I get this to apply to all pages on the site? The code works fine for the index page but not for other pages.
Thank you
February 6, 2017 at 9:08 am #1359725
JoaoModeratorHi There,
Try going to the internal pages and clicking Edit Page > Select the main menu as One page navigation menu on each page.
Let us know how it goes,
Joao
-
AuthorPosts
- <script> jQuery(function($){ $("#no-reply-1359400 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>
