Accordion not scrolling correctly to expanded element

问题: I have accordion and I am trying to scroll to top of active, but now the problem is when I open one and then try to open another the offset not working properly. But if all...

问题:

I have accordion and I am trying to scroll to top of active, but now the problem is when I open one and then try to open another the offset not working properly. But if all of them are collapsed and try to expand some of them it's working. The problem is only when I have expanded one and try to expand another.

<div class="faqs">
   <ul>
     <li class="q"><a href="#"> Question </a></li>
     <li class="a"> Answer </li>
  </ul>

   <ul>
     <li class="q"><a href="#"> Question </a></li>
     <li class="a"> Answer </li>
  </ul>
</div>
jQuery('.faqs ul').each(function(e) {
  var question = jQuery(this).find('li.q a');
  var answer = jQuery(this).find('li.a');

  jQuery(question).click(function(e) {
    e.preventDefault();
    jQuery(this).closest('ul').siblings().removeClass('expanded').find('li.a').slideUp(500);
    jQuery(this).closest('ul').toggleClass("expanded").find('li.a').slideToggle(500);
    jQuery("html,body").animate({scrollTop: jQuery(question).offset().top },300); 
  });
});

回答1:

Wrap animate function inside setTimeout.

  setTimeout(function(){ 
    jQuery('html,body').animate({scrollTop: jQuery(question).offset().top}, 600);
  }, 500);
  • 发表于 2020-06-27 19:22
  • 阅读 ( 103 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除