-
AuthorPosts
-
August 12, 2015 at 5:19 pm #357548
Hello
I´m using anchors to jump to different parts on a page (#nameofid) and it works as expected on desktop but on mobile the link gets clicked as soon as the finger touches it.
Normal links/buttons etc are working fine, but it appears as the smooth scrolling is waiting for a mousedown event which causes troubles when swiping on a mobile device.
I can see it´s the same behaviour as the back to top arrow so I wanted to see if anyone knew what file I needed to modify so that the smooth scrolling is only done when the finger gets released?
Thanks
August 12, 2015 at 8:57 pm #357680Hi There,
Thanks for writing in! Would you mind providing us the site URL so we can take a closer look.
Cheers!
August 13, 2015 at 3:03 am #358044This reply has been marked as private.August 13, 2015 at 3:14 am #358053Hello There,
We would not recommend editing any js file as these will just be overwritten when there is an update of X. Please use this code instead in your customizer, Appearance > Customize > Custom > Javascript
jQuery(document).ready(function($) { // // Scroll trigger. // $('a[href*="#"]').on('touchstart', function(e) { return false; }); $('a[href*="#"]').on('touchend click', function(e) { $href = $(this).attr('href'); notComments = $href.indexOf('#comments') === -1; notAccordion = $href.indexOf('#collapse-') === -1; notTabbedContent = $href.indexOf('#tab-') === -1; if ( $href !== '#' && notComments && notAccordion && notTabbedContent ) { var theId = $href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); animateOffset($el, 850, 'easeInOutExpo'); } } }); });
Please let us know if this works out for you.
August 13, 2015 at 7:10 am #358225Thanks for the quick reply, but unfortunately it did not fix it for me.
I get this error now “Uncaught ReferenceError: animateOffset is not defined”, and the same scrolling behavior is occurring.
Just for fun I tried changing line 200 in /themes/x/framework/js/src/site/inc/x-body-scrollspy.js
from
$(‘a[href*=”#”]’).on(‘touchstart click’, function(e) {to
$(‘a[href*=”#”]’).on(‘touchend click’, function(e) {But that did not change it
August 13, 2015 at 7:11 am #358226This reply has been marked as private.August 13, 2015 at 8:59 am #358294Hi,
Please change the javascript code above to this.
jQuery(document).ready(function($) { function animateOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight + 1 }, ms, easing); } $('a[href*="#"]').on('touchstart', function(e) { return false; }); $('a[href*="#"]').on('touchend click', function(e) { $href = $(this).attr('href'); notComments = $href.indexOf('#comments') === -1; notAccordion = $href.indexOf('#collapse-') === -1; notTabbedContent = $href.indexOf('#tab-') === -1; if ( $href !== '#' && notComments && notAccordion && notTabbedContent ) { var theId = $href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); animateOffset($el, 850, 'easeInOutExpo'); } } }); });
If that doesn’t help, please provide us you wordpress admin login so we could take a closer look.
Thanks
August 13, 2015 at 10:52 am #358366This reply has been marked as private.August 13, 2015 at 2:12 pm #358525Hi there,
I checked your website but I couldn’t find the JS code in your Customizer. However, you can replace the previous provided JS code with this and it should resolve the issue:
jQuery(document).ready(function($) { var adminbarHeight = $('#wpadminbar').outerHeight(); var navbarFixedTopHeight = $('.x-navbar-fixed-top-active .x-navbar').outerHeight(); function animateOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight + 1 }, ms, easing); } // // Scroll trigger. // $('a[href*="#"]').off('touchstart'); $('a[href*="#"]').on('touchend click', function(e) { $href = $(this).attr('href'); notComments = $href.indexOf('#comments') === -1; notAccordion = $href.indexOf('#collapse-') === -1; notTabbedContent = $href.indexOf('#tab-') === -1; if ( $href !== '#' && notComments && notAccordion && notTabbedContent ) { var theId = $href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); animateOffset($el, 850, 'easeInOutExpo'); } } }); });
Hope this helps. 🙂
Thank you.
August 15, 2015 at 10:33 am #359926Awesome thanks!
Did a small modification and replaced touchend with onclick :
jQuery(document).ready(function($) { var adminbarHeight = $('#wpadminbar').outerHeight(); var navbarFixedTopHeight = $('.x-navbar-fixed-top-active .x-navbar').outerHeight(); function animateOffset( element, ms, easing ) { $('html, body').animate({ scrollTop: $(element).offset().top - adminbarHeight - navbarFixedTopHeight + 1 }, ms, easing); } // // Scroll trigger. // $('a[href*="#"]').off('touchstart'); $('a[href*="#"]').on('onclick', function(e) { $href = $(this).attr('href'); notComments = $href.indexOf('#comments') === -1; notAccordion = $href.indexOf('#collapse-') === -1; notTabbedContent = $href.indexOf('#tab-') === -1; if ( $href !== '#' && notComments && notAccordion && notTabbedContent ) { var theId = $href.split('#').pop(); var $el = $('#' + theId); if ( $el.length > 0 ) { e.preventDefault(); animateOffset($el, 850, 'easeInOutExpo'); } } }); });
Now it’s working like I wanted
August 15, 2015 at 12:10 pm #359973Glad you’ve sorted it out.
You’re most welcome 🙂
-
AuthorPosts