简体   繁体   English

单击时将焦点转移到手风琴元素

[英]Shifting focus to accordion element when clicked

I have this accrodion: http://jsfiddle.net/ksqzg3Ld/我有这个手风琴: http://jsfiddle.net/ksqzg3Ld/

P.S: Dont remove the <br> tags. They have been put intentionally

I want to shift the focus to the accordion element when clicked.单击时,我想将焦点转移到手风琴元素。 For eg: When Accordion section #2 or Accordion section #3 is clicked, the scroll should automatically go down and focus on the accordion section data.例如:当 Accordion section #2 或 Accordion section #3 被点击时,滚动应该自动向下 go 并专注于手风琴部分数据。 Is there any way to achieve this?有什么办法可以做到这一点?

Essentially, my accordion will be displayed on my web page with half of it visible to the user from the bottom.本质上,我的手风琴将显示在我的 web 页面上,用户可以从底部看到其中的一半。 I dont want the user to scroll down by himself to read the accordion element data when the element is clicked我不希望用户在单击元素时自行向下滚动以读取手风琴元素数据

 $(document).ready(function(){
     $('.accordion-section-title').click(function(e){
         var currentAttrvalue = $(this).attr('href');
         if($(e.target).is('.active')){
             $(this).removeClass('active');
             $('.accordion-section-content:visible').slideUp(300);
         } else {
             $('.accordion-section-title').removeClass('active').filter(this).addClass('active');
             $('.accordion-section-content').slideUp(300).filter(currentAttrvalue).slideDown(300);
         }
     });
 });

Add this line at the end of the click function: $("html, body").animate({scrollTop: $(currentAttrvalue).offset().top - 10}, 0);在点击的末尾添加这一行 function: $("html, body").animate({scrollTop: $(currentAttrvalue).offset().top - 10}, 0);

 $(document).ready(function(){ $('.accordion-section-title').click(function(e){ var currentAttrvalue = $(this).attr('href'); if($(e.target).is('.active')){ $(this).removeClass('active'); $('.accordion-section-content:visible').slideUp(300); } else { $('.accordion-section-title').removeClass('active').filter(this).addClass('active'); $('.accordion-section-content').slideUp(300).filter(currentAttrvalue).slideDown(300); } $("html, body").animate({scrollTop: $(currentAttrvalue).offset().top - 10}, 0); }); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM