views:

56

answers:

3

I have a modal box with a defined height. Inside of this box there is an accordion box that expands when a user clicks a link. The height of the accordion box when it is toggled exceeds the height of the modal box and thus a scrollbar appears.

I would like to automatically scroll the bars down to its lowest point to display the new content in the accordion box.

The accordian box looks like this:

<script type="text/javascript"> 
$(document).ready(function(){
$(".btn-slide").click(function(){
$("#panel").slideToggle("slow");
$(this).toggleClass("active"); return false;
});
});
</script> 

Is there a way to integrate this scroll down function into the existing function? Is there an existing Jquery function for this type of behavior?

+1  A: 

If I understand what you're looking for...

If you'd like a nice smooth scroll, then the scollTo plugin is a great choice.

If you don't care, just use a hash. location.hash = '#someid';

ScottE
Yeah, this is exactly what I am looking for. I don't really care about any animation, I just need to get down there when the accordion triggers. I would like to avoid using some extra plugin if possible though.
Thomas
+1  A: 

If you don't want to rely on any plugin, or change the actual URL, you could use scrollTop, see http://api.jquery.com/scrollTop/

Cheers.

peol
I thought about this, but the content is dynamic, so I am unsure what the hieght will be.
Thomas
You should have a reference to the wanted element, so you could use $(window).scrollTop( $(wantedelement).offset().top ) -- If I'm not mistaken.
peol
Ok so I set it up like this:$(document).ready(function(){$(".btn-slide").click(function(){$(window).scrollTop( $("#no").offset().top )});});#no is a the the very bottom of my accordion panel. This doesn't seem to fire. Have I set it up correctly?
Thomas
Seems to work for me here; http://jsfiddle.net/ZuBZT/ in Chrome. According to the comments on the API site, `scrollTop` can be a little dodgy. You'll have to try with different selectors (although, `window` seems best), here's another one that people seem to use: `$('body,html')`
peol
Thanks for making an example to look at - Im still having trouble with it. Using your code, I put together my situation here:http://jsfiddle.net/ZuBZT/10/
Thomas
Hm, might be an issue with $(window) (and jsfiddle); try this one: http://jsbin.com/ifege
peol
A: 

The trick is to use an outer container-div which is scrollable. This allows to find out the size of the inner div which holds the actual content.
My solution (You have to do some adjustments for your accordion):


<html>
   <head>
      <style>
         #container {
            overflow-y: scroll;
            height: 200px; /* defined height */
         }
      </style>
   </head>
   <body>
      <div id="container">
         <div id="content">
            <!-- dynamic content -->
         </div>
      </div>
   </body>
   <script type="text/javascript" src="http://jquery.com/src/jquery-latest.js&quot; ></script>
   <script type="text/javascript">
      function onNewContent() {
         $("#container").scrollTop($("#content").height());
      }

      //test
      $(document).ready(function() {
         for(var i = 0; i < 500; ++i)
            $("#content").append($("<div/>").html(i));
         onNewContent();
      });
   </script>
</html>
fishbone