Rob Murrer

JQuery Scroller

In one of the previous iterations of this site I had a User Interface (UI) element that had a scrollable element that was using this code.

jquery.vscroll.plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(function($){

$.fn.vscroll = function(options) {
  var opts = $.extend({}, $.fn.vscroll.defaults, options);
  
  return this.each(function() {

      var $scroll_item = $(this).find(opts.vscroll_toscroll);
      var $scroll_up = $(this).find(opts.vscroll_up);
      var $scroll_down = $(this).find(opts.vscroll_down);
      var vscroll_pos = 0;
      
      //hide DOWN  if the element if we don't need it 
      if ($scroll_item.height() <= $scroll_item.parent().height() ) { $scroll_down.css("visibility", "hidden"); }
                              
      $scroll_up.click(function(event) {
          if (vscroll_pos >= 0) { $scroll_up.css("visibility", "hidden"); }
          else { vscroll_pos += opts.vscroll_size; }
          $scroll_item.animate({top: vscroll_pos},"normal")
          if ($scroll_down.css("visibility") == "hidden") { $scroll_down.css("visibility", "visible"); }  //if hidden show DOWN element
          if (vscroll_pos >= 0) { $scroll_up.css("visibility", "hidden"); } // if visible hide UP element
           event.preventDefault();
          });
          
      $scroll_down.click(function(event) {
          vscroll_pos -=  opts.vscroll_size;
          $scroll_item.animate({top: vscroll_pos},"normal")
          if ($scroll_up.css("visibility") == "hidden") { $scroll_up.css("visibility", "visible"); }  //if hidden show UP element
          if ($scroll_item.height() <= ( Math.abs(vscroll_pos) + $scroll_item.parent().height() ) ) { $scroll_down.css("visibility", "hidden"); }  //hide DOWN - make sure don't scroll to far also checking containers height
          event.preventDefault();
      });
          
  });
};


$.fn.vscroll.defaults = {
   vscroll_toscroll: ".vscroll_toscroll",
   vscroll_up: ".vscroll_up",
   vscroll_down: ".vscroll_down",
   vscroll_size: 84
};

})(jQuery);

Usage

Scrollable Div
1
2
3
4
5
6
7
8
9
10
11
12
13
<div id="vscroll">
    <div class="footer_scroller">
      <ul class="vscroll_toscroll">
            <li><a href="#">Lorem ipsum</a></li>
            <li><a href="#">Lorem ipsum</a></li>
            <li><a href="#">Lorem ipsum</a></li>
            <li><a href="#">Lorem ipsum</a></li>
            <li><a href="#">Lorem ipsum</a></li>
      </ul>
    </div>
    <a class="vscroll_up" href="#">Less</a>
    <a class="vscroll_down" href="#">More</a>
</div>

Then in your $(document).ready(function()) place $("#vscroll").vscroll();.

A live version soon.