Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason it only works on the first click is because you are only ever moving +48px or -48px from the top. You need to +/- the 48 from the current scrollTop value.</p> <p>So if we're already 96px from the top and we press down, we want to add 48 to 96 like so:</p> <p><a href="http://jsfiddle.net/XGXvD/2/" rel="noreferrer">jsfiddle here</a></p> <pre><code> $(document).on("click", "#down", function(){ var scrollval = $('.child').height(); // this gives us the current scroll position var currentscrollval = $('#parent').scrollTop(); $('#parent').scrollTop(scrollval+currentscrollval); }); </code></pre> <p>Also, note that <code>.live()</code> has been depreciated from jQuery 1.7+. You need to use <code>.on()</code> like my example (if you are using jQuery 1.7+)</p> <h2>EDIT - Added Show/Hide buttons functionality</h2> <p><a href="http://jsfiddle.net/XGXvD/4/" rel="noreferrer">Updated jsfiddle here</a></p> <p>First, you need to work out some variables. I declared these outside of the click events so they could be used in both functions if needed.</p> <pre><code>// get the number of .child elements var totalitems = $("#parent .child").length; // get the height of .child var scrollval = $('.child').height(); // work out the total height. var totalheight = (totalitems*scrollval)-($("#parent").height()); </code></pre> <p><strong>Up:</strong></p> <pre><code>// hide/show buttons if(currentscrollval == totalheight) { $(this).hide(); } else { $("#up").show(); } </code></pre> <p><strong>Down:</strong></p> <pre><code> // hide/show buttons if((scrollval+currentscrollval) == scrollval) { $(this).hide(); } else { $("#down").show(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload