Kadang-kadang bisa sangat berguna untuk menyembunyikan scrollbar dalam sebuah situs web dan hanya terlihat ketika pengguna benar-benar membutuhkannya. The real-time aktivitas pakan di Facebook adalah contoh untuk perilaku seperti itu. Hari ini saya ingin menunjukkan kepada Anda bagaimana menggunakan JScrollPane dan memperpanjang dengan fungsi untuk menyembunyikan scrollbar dan menampilkannya ketika Container onmouse hover / saat mouse diarahkan pada content yang di targetkan menggunakan Scrollbar.

Scrollbar Visibility with jScrollPane



JScrollPane diciptakan oleh Kevin Luck dan Anda dapat membaca lebih lanjut di sini:
JScrollPane - cross scrollbar browser yang styleable dengan jQuery dan CSS

THE MARKUP

Contoh untuk markup yang digunakan dalam demo saya adalah sebagai berikut:
  1. <div id="jp-container" class="jp-container">
  2. <!-- content -->
  3. </div>
Tag pada jp-kontainer tersebut yang akan discroll ke area konten.

CSS

Selain style untuk custom scrollbar JScrollPane, kita akan membuat Container Style:
  1. .jp-container{
  2. width:500px;
  3. height:400px;
  4. position:relative;
  5. background:#fff;
  6. border:1px solid #D8DFEA;
  7. float:left;
  8. }

JAVASCRIPT

Kita akan menggunakan plugin JScrollPane dengan fungsi baru untuk menampilkan dan menyembunyikan scrollbar:
  1. // the element we want to apply the jScrollPane
  2. var $el = $('#jp-container').jScrollPane({
  3. verticalGutter : -16
  4. }),
  5. // the extension functions and options
  6. extensionPlugin = {
  7. extPluginOpts : {
  8. // speed for the fadeOut animation
  9. mouseLeaveFadeSpeed : 500,
  10. // scrollbar fades out after
  11. // hovertimeout_t milliseconds
  12. hovertimeout_t : 1000,
  13. // if set to false, the scrollbar will
  14. // be shown on mouseenter and hidden on
  15. // mouseleave
  16. // if set to true, the same will happen,
  17. // but the scrollbar will be also hidden
  18. // on mouseenter after "hovertimeout_t" ms
  19. // also, it will be shown when we start to
  20. // scroll and hidden when stopping
  21. useTimeout : false,
  22. // the extension only applies for devices
  23. // with width > deviceWidth
  24. deviceWidth : 980
  25. },
  26. hovertimeout : null,
  27. // timeout to hide the scrollbar
  28. isScrollbarHover: false,
  29. // true if the mouse is over the scrollbar
  30. elementtimeout : null,
  31. // avoids showing the scrollbar when moving
  32. // from inside the element to outside, passing
  33. // over the scrollbar
  34. isScrolling : false,
  35. // true if scrolling
  36. addHoverFunc : function() {
  37. // run only if the window has a width bigger than deviceWidth
  38. if( $(window).width() <= this.extPluginOpts.deviceWidth ) return false;
  39. var instance = this;
  40. // functions to show / hide the scrollbar
  41. $.fn.jspmouseenter = $.fn.show;
  42. $.fn.jspmouseleave = $.fn.fadeOut;
  43. // hide the jScrollPane vertical bar
  44. var $vBar = this.getContentPane().siblings('.jspVerticalBar').hide();
  45. /*
  46. * mouseenter / mouseleave events on the main element
  47. * also scrollstart / scrollstop
  48. * @James Padolsey : http://james.padolsey.com/javascript/special-scroll-events-for-jquery/
  49. */
  50. $el.bind('mouseenter.jsp',function() {
  51. // show the scrollbar
  52. $vBar.stop( true, true ).jspmouseenter();
  53. if( !instance.extPluginOpts.useTimeout ) return false;
  54. // hide the scrollbar after hovertimeout_t ms
  55. clearTimeout( instance.hovertimeout );
  56. instance.hovertimeout = setTimeout(function() {
  57. // if scrolling at the moment don't hide it
  58. if( !instance.isScrolling )
  59. $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  60. }, instance.extPluginOpts.hovertimeout_t );
  61. }).bind('mouseleave.jsp',function() {
  62. // hide the scrollbar
  63. if( !instance.extPluginOpts.useTimeout )
  64. $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  65. else {
  66. clearTimeout( instance.elementtimeout );
  67. if( !instance.isScrolling )
  68. $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  69. }
  70. });
  71. if( this.extPluginOpts.useTimeout ) {
  72. $el.bind('scrollstart.jsp', function() {
  73. // when scrolling show the scrollbar
  74. clearTimeout( instance.hovertimeout );
  75. instance.isScrolling = true;
  76. $vBar.stop( true, true ).jspmouseenter();
  77. }).bind('scrollstop.jsp', function() {
  78. // when stop scrolling hide the
  79. // scrollbar (if not hovering it at the moment)
  80. clearTimeout( instance.hovertimeout );
  81. instance.isScrolling = false;
  82. instance.hovertimeout = setTimeout(function() {
  83. if( !instance.isScrollbarHover )
  84. $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  85. }, instance.extPluginOpts.hovertimeout_t );
  86. });
  87. // wrap the scrollbar
  88. // we need this to be able to add
  89. // the mouseenter / mouseleave events
  90. // to the scrollbar
  91. var $vBarWrapper = $('<div>').css({
  92. position : 'absolute',
  93. left : $vBar.css('left'),
  94. top : $vBar.css('top'),
  95. right : $vBar.css('right'),
  96. bottom : $vBar.css('bottom'),
  97. width : $vBar.width(),
  98. height : $vBar.height()
  99. }).bind('mouseenter.jsp',function() {
  100. clearTimeout( instance.hovertimeout );
  101. clearTimeout( instance.elementtimeout );
  102. instance.isScrollbarHover = true;
  103. // show the scrollbar after 100 ms.
  104. // avoids showing the scrollbar when moving
  105. // from inside the element to outside,
  106. // passing over the scrollbar
  107. instance.elementtimeout = setTimeout(function() {
  108. $vBar.stop( true, true ).jspmouseenter();
  109. }, 100 );
  110. }).bind('mouseleave.jsp',function() {
  111. // hide the scrollbar after hovertimeout_t
  112. clearTimeout( instance.hovertimeout );
  113. instance.isScrollbarHover = false;
  114. instance.hovertimeout = setTimeout(function() {
  115. // if scrolling at the moment
  116. // don't hide it
  117. if( !instance.isScrolling )
  118. $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  119. }, instance.extPluginOpts.hovertimeout_t );
  120. });
  121. $vBar.wrap( $vBarWrapper );
  122. }
  123. }
  124. },
  125. // the jScrollPane instance
  126. jspapi = $el.data('jsp');
  127. // extend the jScollPane by merging
  128. $.extend( true, jspapi, extensionPlugin );
  129. jspapi.addHoverFunc();
Terakhir semoga tutorial ini dapat memberikan Anda inspirasi.
Terimakasih sudah berkunjung dan membaca artikel mengenai Scrollbar Visibility with jScrollPane