2

So I'm working on a list that you can scroll through by clicking on buttons. And it also needs to have a scrollbar visible and working too. But I don't know how to edit my code to get them both to function. I can either have the buttons working or the scrollbar, not both. Can someone help?

var itemsToShow = 3;

$('#scroll>li').each(function(i,k) {
    var ele = $(this);
    $(ele).attr('id', 'scroll' + i);

});


$('#up').bind('click', function() {
    if ($('#scroll0:hidden').length > 0)
    {
        // This means we can go up
        var boundaryTop = $('ul li:visible:first').attr('id');
        var boundaryBottom = $('ul li:visible:last').attr('id');

        if ($('ul li#'+ boundaryTop).prev().length > 0)
        {
            $('ul li#'+ boundaryTop).prev().show();
            $('ul li#'+ boundaryBottom).hide();
        }
    }
});

$('#down').bind('click', function() {
    if ($('#scroll li:last:hidden').length > 0)
    {
        // This means we can go down
        var boundaryTop = $('#scroll li:visible:first').attr('id');
        var boundaryBottom = $('#scroll li:visible:last').attr('id');

        if ($('#scroll li#'+ boundaryBottom).next().length > 0)
        {
            $('#scroll li#'+ boundaryBottom).next().show();
            $('#scroll li#'+ boundaryTop).hide();
        }
}
});
.lg {
    overflow-x:auto; 
    height:90px;
    overflow-y:auto;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="lg">
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
  <li>Text</li>
</ul>

<div id="updown">
  <a class="btn btn-primary" id="up" href="#">up</a>
<a class="btn btn-primary" id="down" href="#">down</a>
</div>

What am I doing wrong? Someone please help!

Thank you!

5
  • Retros, do you want to it click through the divs? Or scroll to the next group? Commented Apr 5, 2017 at 17:06
  • Hey ether, I want to scroll to the next li. Commented Apr 5, 2017 at 17:08
  • $('#scroll0:hidden').length is always give you 0. where is element with #scroll0 Id Commented Apr 5, 2017 at 17:11
  • So, you need to set a div to an id of scroll because in your example, your lis don't gain an id. Also, you aren't setting a :hidden state anywhere Commented Apr 5, 2017 at 17:13
  • Thank you ether! Commented Apr 5, 2017 at 17:48

3 Answers 3

4

Here is how you scroll to top and bottom:

https://jsfiddle.net/45cmhys8/

// Scroll to the top
$('a#up').on('click', function(){
   $('.lg').animate({ scrollTop: 0 }, 600);
})
// Scroll to the bottom
$('a#down').on('click', function(){
   $(".lg").animate({ scrollTop:$(document).height()}, 600);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Simple, but effective! Thanks Keith!
2

You can use the scrollTop() function in jQuery to do this in a one-liner.

10 is the amount of pixels you want to jump up/down.

$('#up').click(function(){
  $('ul.lg').scrollTop($('ul.lg').scrollTop()- 10);
});

$('#down').click(function(){
  $('ul.lg').scrollTop($('ul.lg').scrollTop() + 10);
});

Comments

0

It looks like you are trying to literally show/hide the fields, which removes them from the flow of the browser. Rather then show/hide, you should probably set the scrollTop position of the scrollable div using javascript, rather then manipulate the elements inside of it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.