Answer the question
In order to leave comments, you need to log in
How to hide bootstrap menu on click?
Good day! On a bootstrap menu site, it works on anchors. I want that when you click on the link, it would accordingly transfer to the desired part of the page and hide. I tried to remove the in class with js but then it doesn't transition correctly. Here is the js that is responsible for the smooth transition:
$("#menu").on("click","a", function (event) {
event.preventDefault();
var id = $(this).attr('href'),
top = $(id).offset().top;
$('body,html').animate({scrollTop: top}, 600);
// $('#bs-example-navbar-collapse-1').removeClass('in'); Вот этот класс у меня стоял которым я убирал класс in
});
Answer the question
In order to leave comments, you need to log in
since there is no complete code ... let's try to treat the photo.
var $scrollTo; // для передачи данных между функциями сохраняем до какого объекта будем скроллить
$("#menu").on("click","a", function (event) {
event.preventDefault();
$scrollTo= $($(this).attr('href'));
$("#menu").collapse(); // сворачиваем меню тут координаты могут поменяться во время сворачивания
});
$('#menu').on('hidden.bs.collapse', function () { // обработчик события уже после того, как меню свернулось
var top = $scrollTo.offset().top; // получили куда скроллить не в объектах, а в пикселях
$('body,html').animate({scrollTop: top}, 600);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question