Чтобы прокрутить до привязки с помощью jQuery с плавным эффектом, вы можете использовать различные методы. Вот несколько возможных подходов:
-
Метод 1. Использование функции animate()
$('a[href^="#"]').on('click', function(event) { var target = $(this.getAttribute('href')); if (target.length) { event.preventDefault(); $('html, body').animate({ scrollTop: target.offset().top }, 1000); // Adjust the animation speed as needed } }); -
Метод 2: использование функции ScrollTo()
$('a[href^="#"]').on('click', function(event) { event.preventDefault(); var target = $(this.getAttribute('href')); if (target.length) { $('html, body').stop().animate({ scrollTop: target.offset().top }, 1000); // Adjust the animation speed as needed } }); -
Метод 3. Реализация плавной прокрутки с помощью специальной функции замедления
$('a[href^="#"]').on('click', function(event) { var target = $(this.getAttribute('href')); if (target.length) { event.preventDefault(); $('html, body').stop().animate({ scrollTop: target.offset().top }, 1000, 'swing'); // Adjust the animation speed and easing as needed } });
Эти методы обеспечат плавную прокрутку к якорю при нажатии соответствующей ссылки. Не забудьте заменить 'a[href^="#"]'соответствующим селектором для ваших якорных ссылок.