javascript - How to scroll to top of element with collapsible panels with Jquery -
i have series of divs on click, toggle state of collapsible div (like accordion widget). working fine, want able scroll top of trigger div (the div class name 'paneltab') when user clicks. problem when panels hidden, take no space jquery doesn't automatically 'know' size of page, , isn't able calculate top of element automatically. happens on click scrolls, 'overshoots' top of window , lands in middle of paragraph content. i'm stuck - know simple solution? in advance.
<div class="paneltab first holder"> <div class="text-block"> <h3>lorem ipsum</h3><p class="sub-title">lorem ipsum dolor</p> </div> <div class="clearfix"></div> </div> <div class="animatedpanel"> <p>lorem ipsum dolor sit amet, consectetur adipiscing elit. fusce ornare ornare lectus in pulvinar. donec tempor odio sit amet phare- tra commodo. suspendisse sem libero, tempor vitae egestas eu, sagittis sit amet metus. sed congue tristique quam in gravida. sed cursus, lectus vel rhoncus bibendum, erat ante mattis mauris, ac pharetra erat mauris nec turpis. sed tincidunt aliquam est eget rhoncus. proin eget metus ex. sed sit amet eros feugiat, dignissim purus eget, rhoncus augue. vestibulum lacinia tellus vel turpis vestibulum pharetra. lestie, viverra mauris nec, semper turpis. nunc accumsan augue ut ligula iaculis auctor. </p></div> <div class="paneltab holder"> <div class="text-block" > <h3>lorem ipsum </h3><p class="sub- title">lorem ipsum dolor</p> </div> <div class="clearfix"></div></div> <div class="animatedpanel"><p>lorem ipsum dolor sit amet, consectetur adipiscing elit. fusce ornare ornare lectus in pulvinar. donec tempor odio sit amet phare- tra commodo. suspendisse sem libero, tempor vitae egestas eu, sagittis sit amet metus. sed congue tristique quam in gravida. sed cursus, lectus vel rhoncus bibendum, erat ante mattis mauris, ac pharetra erat mauris nec turpis. sed tincidunt aliquam est eget rhoncus. proin eget metus ex. sed sit amet eros feugiat, dignissim purus eget, rhoncus augue. vestibulum lacinia tellus vel turpis vestibulum pharetra. lestie, viverra mauris nec, semper turpis. nunc accumsan augue ut ligula iaculis auctor. </p></div> <div class="paneltab holder"><div class="text-block" > <h3>lorem ipsum</h3><p class="sub-title">lorem ipsum dolor</p> </div><div class="clearfix"></div></div> <div class="animatedpanel"><p>lorem ipsum dolor sit amet, consectetur adipiscing elit. fusce ornare ornare lectus in pulvinar. donec tempor odio sit amet phare- tra commodo. suspendisse sem libero, tempor vitae egestas eu, sagittis sit amet metus. sed congue tristique quam in gravida. sed cursus, lectus vel rhoncus bibendum, erat ante mattis mauris, ac pharetra erat mauris nec turpis. sed tincidunt aliquam est eget rhoncus. proin eget metus ex. sed sit amet eros feugiat, dignissim purus eget, rhoncus augue. vestibulum lacinia tellus vel turpis vestibulum pharetra. lestie, viverra mauris nec, semper turpis. nunc accumsan augue ut ligula iaculis auctor.</p></div> <div class="paneltab holder"> <div class="text-block" > <h3>lorem ipsum</h3><p class="sub-title">lorem ipsum dolor</p> </div><div class="clearfix"></div></div> <div class="animatedpanel"><p>lorem ipsum dolor sit amet, consectetur adipiscing elit. fusce ornare ornare lectus in pulvinar. donec tempor odio sit amet phare- tra commodo. suspendisse sem libero, tempor vitae egestas eu, sagittis sit amet metus. sed congue tristique quam in gravida. sed cursus, lectus vel rhoncus bibendum, erat ante mattis mauris, ac pharetra erat mauris nec turpis. sed tincidunt aliquam est eget rhoncus. proin eget metus ex. sed sit amet eros feugiat, dignissim purus eget, rhoncus augue. vestibulum lacinia tellus vel turpis vestibulum pharetra. lestie, viverra mauris nec, semper turpis. nunc accumsan augue ut ligula iaculis auctor.</p></div>
and jquery:
$('.animatedpanel').hide(); $('.paneltab').click(function() { $('.paneltab').removeclass('active'); $(this).toggleclass('active'); var panel = $(this).next() $('.animatedpanel').not(panel).slideup(); panel.slidetoggle({ direction: "up" }, 100); $('html, body').animate({ scrolltop: $(this).offset().top }, 200); });
try changing style of hidden element position:absolute, display: block, visibility:hidden calculate height , restore old style
Comments
Post a Comment