Tengo el siguiente código en nuestro repetitivo que parece funcionar bien en la configuración actual. Se desplaza a la sección de la página cuando hace clic en el enlace del menú de navegación o en un enlace en un control deslizante. Lo que encontré difícil de resolver es hacer que funcione al navegar desde otra página a la página de inicio. Agregará el hash #discover a la URL muy bien, por ejemplo, pero no lo registrará por alguna razón. Ha pasado un tiempo desde que usé correctamente jQuery 😅.
Ya he agregado el estándar en el código listo para documentos alrededor de la función con la esperanza de que sea eso, pero no hay dados. Espero que alguien pueda darme una pista sobre lo que está pasando aquí.
$(function(){ }); /* Only enable this when you want smooth scrolling! Usage: - Give the anchor you want to scroll to a certain position a href which starts with # and the class "scroll" (<a href="#container" class="scroll"></a>) - Give the container you want to scroll to a name (<section name="container"></section) - ??? - profit */ const scrollOffset = 0 // the amount of pixels the animation should scroll "extra" (When you have a sticky navigation for example) $(function () { $( '.menu__item a[href*="#"]:not([href="#"]), .slider-list__link[href*="#"]:not([href="#"])' ).click(function () { if ( location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname ) { var target = $(this.hash) target = target.length ? target : $('[name=' + this.hash.slice(1) + ']') $('body').removeClass('no-scroll') $('.navbar').removeClass('navbar--is-open') if (target.length) { setTimeout(() => { // This is used to prevent the animation from the menu closing from interfering with the scrolling animation. It could mess up the offsets otherwise $('html, body').animate( { scrollTop: target.offset().top + scrollOffset, // Scroll to the target }, 400 ) return false }, 500) } } }) })