jquery - Scrolling to content after a Bootstrap Accordion is opened -
i have bootstrap accordion has many panels in it. issue facing if panel opens off page user has no idea panel open , can scroll down it.
to solve wanted able scroll content open know content open , saves them scrolling it.
i seem running issues though when trying attempt this.
this function looks like
$('.acclink').on('click', function(){ if($(this).parent().next('.panel-collapse').hasclass('in')){ }else{ //this scroll happen } });
so far have tried...
$('html, body').animate({ scrolltop: ($(this).parent().next('.panel-collapse')) },500);
and
$('div').animate({ scrolltop: ($(this).parent().next('.panel-collapse')) },500);
i have tried this...
function scrolltoelement(ele) { $(window).scrolltop(ele.offset().top).scrollleft(ele.offset().left); } var element = $(this).parent().next('.panel-collapse').attr('id'); scrolltoelement($('#'+element));
but neither page. sits there. appreciated!
rather separate click event listener, use bootstrap's built-in event callback:
$("#accordion").on("shown.bs.collapse", function () { var myel = $(this).find('.collapse.in'); $('html, body').animate({ scrolltop: $(myel).offset().top }, 500); });
or, if want heading show:
$("#accordion").on("shown.bs.collapse", function () { var myel = $(this).find('.collapse.in').prev('.panel-heading'); $('html, body').animate({ scrolltop: $(myel).offset().top }, 500); });
Comments
Post a Comment