其他代码

滚动条美化jQuery插件

阿里云

有时候隐藏滚动条对于用户体验来说是一件非常不错的事情,当需要的时候才显示它。这款插件就是使用了 jScrollPane 和 jQuery 来实现这种滚动条显示和隐藏的美化效果。
如果你想了解更多关于 jScrollPane 的知识,请点击这里:jScrollPane – cross browser styleable scrollbars with jQuery and CSS。

HTML 结构

  1. <div id="jp-container" class="jp-container">
  2.     <!-- content -->
  3. </div>
也想出现在这里?联系我们
创客主机
  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

  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.  
  23.             // the extension only applies for devices 
  24.             // with width > deviceWidth
  25.             deviceWidth         : 980
  26.         },
  27.         hovertimeout    : null, 
  28.         // timeout to hide the scrollbar        
  29.         isScrollbarHover: false,
  30.         // true if the mouse is over the scrollbar      
  31.         elementtimeout  : null, 
  32.         // avoids showing the scrollbar when moving 
  33.         // from inside the element to outside, passing 
  34.         // over the scrollbar       
  35.         isScrolling     : false,
  36.         // true if scrolling        
  37.         addHoverFunc    : function() {  
  38.             // run only if the window has a width bigger than deviceWidth
  39.             if( $(window).width() <= this.extPluginOpts.deviceWidth ) return false;
  40.             var instance        = this;         
  41.             // functions to show / hide the scrollbar
  42.             $.fn.jspmouseenter  = $.fn.show;
  43.             $.fn.jspmouseleave  = $.fn.fadeOut;
  44.  
  45.             // hide the jScrollPane vertical bar
  46.             var $vBar           = this.getContentPane().siblings('.jspVerticalBar').hide();         
  47.             /*
  48.              * mouseenter / mouseleave events on the main element
  49.              * also scrollstart / scrollstop 
  50.              * @James Padolsey : http://james.padolsey.com/javascript/special-scroll-events-for-jquery/
  51.              */
  52.             $el.bind('mouseenter.jsp',function() {              
  53.                 // show the scrollbar
  54.                 $vBar.stop( true, true ).jspmouseenter();               
  55.                 if( !instance.extPluginOpts.useTimeout ) return false;              
  56.                 // hide the scrollbar after hovertimeout_t ms
  57.                 clearTimeout( instance.hovertimeout );
  58.                 instance.hovertimeout   = setTimeout(function() {
  59.                     // if scrolling at the moment don't hide it
  60.                     if( !instance.isScrolling )
  61.                         $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  62.                 }, instance.extPluginOpts.hovertimeout_t );     
  63.             }).bind('mouseleave.jsp',function() {
  64.  
  65.                 // hide the scrollbar
  66.                 if( !instance.extPluginOpts.useTimeout )
  67.                     $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  68.                 else {
  69.                 clearTimeout( instance.elementtimeout );
  70.                 if( !instance.isScrolling )
  71.                         $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  72.                 }               
  73.             }); 
  74.             if( this.extPluginOpts.useTimeout ) {               
  75.                 $el.bind('scrollstart.jsp', function() {                
  76.                     // when scrolling show the scrollbar
  77.                     clearTimeout( instance.hovertimeout );
  78.                     instance.isScrolling    = true;
  79.                     $vBar.stop( true, true ).jspmouseenter();                   
  80.                 }).bind('scrollstop.jsp', function() {                  
  81.                     // when stop scrolling hide the 
  82.                     // scrollbar (if not hovering it at the moment)
  83.                     clearTimeout( instance.hovertimeout );
  84.                     instance.isScrolling    = false;
  85.                     instance.hovertimeout   = setTimeout(function() {
  86.                         if( !instance.isScrollbarHover )
  87.                             $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  88.                     }, instance.extPluginOpts.hovertimeout_t );
  89.  
  90.                 });
  91.  
  92.                 // wrap the scrollbar
  93.                 // we need this to be able to add 
  94.                 // the mouseenter / mouseleave events 
  95.                 // to the scrollbar
  96.                 var $vBarWrapper    = $('<div/>').css({
  97.                     position    : 'absolute',
  98.                     left        : $vBar.css('left'),
  99.                     top         : $vBar.css('top'),
  100.                     right       : $vBar.css('right'),
  101.                     bottom      : $vBar.css('bottom'),
  102.                     width       : $vBar.width(),
  103.                     height      : $vBar.height()
  104.                 }).bind('mouseenter.jsp',function() {                   
  105.                     clearTimeout( instance.hovertimeout );
  106.                     clearTimeout( instance.elementtimeout );                    
  107.                     instance.isScrollbarHover   = true;
  108.                     // show the scrollbar after 100 ms.
  109.                     // avoids showing the scrollbar when moving 
  110.                     // from inside the element to outside, 
  111.                     // passing over the scrollbar                               
  112.                     instance.elementtimeout = setTimeout(function() {
  113.                         $vBar.stop( true, true ).jspmouseenter();
  114.                     }, 100 );   
  115.  
  116.                 }).bind('mouseleave.jsp',function() {
  117.  
  118.                     // hide the scrollbar after hovertimeout_t
  119.                     clearTimeout( instance.hovertimeout );
  120.                     instance.isScrollbarHover   = false;
  121.                     instance.hovertimeout = setTimeout(function() {
  122.                         // if scrolling at the moment 
  123.                         // don't hide it
  124.                         if( !instance.isScrolling )
  125.                             $vBar.stop( true, true ).jspmouseleave( instance.extPluginOpts.mouseLeaveFadeSpeed || 0 );
  126.                     }, instance.extPluginOpts.hovertimeout_t ); 
  127.                 });         
  128.                 $vBar.wrap( $vBarWrapper );
  129.             }
  130.         }   
  131.     },
  132.     // the jScrollPane instance
  133.     jspapi          = $el.data('jsp');
  134. // extend the jScollPane by merging 
  135. $.extend( true, jspapi, extensionPlugin );
  136. jspapi.addHoverFunc();

滚动条美化 jQuery 插件

已有 447 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!