Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll the div content when clicking a up or down button [duplicate]

I want to create a div like below:

<div id="upButton">&nbsp;</div>
<div id="divWithExcessiveContent" style="overflow: hidden; height: 20px;">
    content number 1<br/>
    content number 2<br/>
    content number 3<br/>
    content number 4<br/>
    ...
    content number 100<br/>
</div>
<div id="downButton">&nbsp;</div>

Based on the code above, #divWithExcessiveContent's content should only be until, perhaps "content number 20" and the rest are hidden. When #downButton is clicked, the inside of the div should scroll down, displaying the content below "content number 20".

How can I do that using jQuery?

like image 510
Henry Gunawan Avatar asked Jun 26 '13 15:06

Henry Gunawan


1 Answers

I've created two jsFiddles for you. Hope this is what you want to do with your item list

You can do the trick by introducing another container div and adjust the height/margin top & bottom to scroll/show next.

<div id="upButton">&nbsp;</div>
<div id="container" style="overflow: hidden;height: 40px;">
<div id="divWithExcessiveContent" >
    content number 1<br/>
    content number 2<br/>
    content number 3<br/>
    content number 4<br/>
    content number 5<br/>
    ...
    content number 100<br/>
</div>
</div>
<br>
<div id="downButton" style="border:solid 1px;width:50px;">Button</div>

Jquery:

var i = 0;
$('div#downButton').click( function() {
    i = i - 20;
   $('div#divWithExcessiveContent').css('margin-top', i + 'px');
  });

i) Scrolling like behavior demo

http://jsfiddle.net/vendettamit/4xuV4/

ii) Expandable div to show next element demo

http://jsfiddle.net/vendettamit/kmHPk/

like image 147
vendettamit Avatar answered Sep 23 '22 02:09

vendettamit