使用JQ来实现浏览器滚动条

来源:互联网 发布:拜纳姆生涯数据 编辑:程序博客网 时间:2024/05/18 02:08

通过修改CSS来更换s滚动条样式,只能更换颜色,而不能更改滚动条的大小。

况且还有浏览器兼容的问题:

 

我们可以选择jquery来做该效果,这样即修改了滚动条的大小,又可以做到兼容:

 

JS代码如下(附:jquery.linscroll.js):

<script type="text/javascript" src="jquery-1.1.3.1.js"></script>
<script type="text/javascript" src="jquery.linscroll.js"></script>
<script type="text/javascript">
$(document).ready(
function(){
$(’#scrollContent’).setScroll( //scrollContent为滚动层的ID
{img:scroll_bk.gif’,width:10},//背景图及其宽度
{img:scroll_arrow_up.gif’,height:3},//up image
{img:scroll_arrow_down.gif’,height:3},//down image
{img:scroll_bar.gif’,height:25}//bar image
);});  
</script>


html代码如下:

<div id="scrollContent" style="width:140px;overflow:hidden;height:170px;">
内容
</div> 


 

jquery.linscroll.js 代码:


 

/*
 * LinScroll-http://uicss.cn/
 * Version 2.1.2008080701
 
 * Copyright (c) 2008 cuikai(uicss.cn)
 *
 * To Run This Code jQuery 1.1.3.1(http://www.jQuery.com) Required.
 *
 *
 */

jQuery.fn.setScroll = function(_scroll,_scroll_up,_scroll_down,_scroll_bar){
    this.each(function(){
       
        var _bar_margin = 3;
       
        //create scroll dom
        var _scroll_control = jQuery('<div class="scroll_zone">').width(_scroll.width).css({'position':'absolute','float':'none',margin:0,padding:0}).css('background','url('+_scroll.img+')');
        var _scroll_control_up = jQuery('<img class="scroll_down">').attr('src',_scroll_up.img).width(_scroll.width).css({'z-index':'1000','position':'absolute', 'top':'0','float':'none',margin:0,padding:0});
        var _scroll_control_down = jQuery('<img class="scroll_down">').attr('src',_scroll_down.img).width(_scroll.width).css({'z-index':'1000','position':'absolute', 'bottom':'0','float':'none',margin:0,padding:0});
        var _scroll_control_bar =  jQuery('<img class="scroll_bar">').attr('src',_scroll_bar.img).width(_scroll.width).css({'z-index':'1500','position':'absolute','float':'none',margin:0,padding:0,height:_scroll_bar.height+'px'}).css('top',_scroll_up.height+_bar_margin+'px');
       
        _scroll_control.append(_scroll_control_up);
        _scroll_control.append(_scroll_control_bar);
        _scroll_control.append(_scroll_control_down);
       
        var _oheight = jQuery(this).css('height').substring(0,jQuery(this).css('height').indexOf('px'));
        var _owidth = jQuery(this).width();
        var _ocontent = jQuery(this).html();
       
        if(jQuery(this).attr('scrollHeight')<=_oheight) return;
       
        var _content_zone = jQuery('<div>').html(_ocontent).css({ width:_owidth-10+'px',height:_oheight+'px',overflow:'hidden','float':'none',margin:0,padding:0});
       
        jQuery(this).css({'overflow':'hidden'});
        jQuery(this).empty().append(_content_zone).css({position:'relative'}).append(_scroll_control.css({left:_owidth-_scroll.width+'px',top:'0',height:_oheight+'px',margin:0,padding:0}));

        //register drag event
        jQuery(this).find('.scroll_bar')
        .mousedown(
            function(){
                jQuery(document).mousemove(
                    function(e){
                      var _content = _content_zone.get(0);
                      var lastProgress = _scroll_control_bar.attr('progress');
                      _scroll_control_bar.attr('progress',e.pageY);
                      var nowProgress = _scroll_control_bar.css('top');
                      nowProgress = nowProgress.substring(0,nowProgress.indexOf('px'));
                      nowProgress = Number(nowProgress) + Number(e.pageY-lastProgress);
                      var preProgress = nowProgress/(_oheight-_scroll_up.height-_scroll_down.height-_scroll_bar.height-(2*_bar_margin));
                      _content.scrollTop = ((_content.scrollHeight - _content.offsetHeight) * preProgress);
                      if(nowProgress<(_scroll_up.height+_bar_margin) || nowProgress > (_oheight-(_scroll_down.height+_scroll_bar.height+_bar_margin))) return false;
                      try{_scroll_control_bar.css('top',nowProgress+'px');}catch(e){}
                      return false;
                    }
                );
                return false;
            }
        )
        .mouseout(
            function(){
                jQuery(document).mouseup(
                    function(){
                        jQuery(document).unbind('mousemove');
                     }
                )
            }
        )
       
    });
}

 

原创粉丝点击