朱羽佳 通过本文主要向大家介绍了jQuery,自定义,滚动条等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了jQuery实现的自定义滚动条。分享给大家供大家参考,具体如下:
可以自由的给滚动条定义背景,上下按钮,当然不仅仅是颜色,连图片当背景也可以。支持鼠标滚轮,点击滚动条滚轴定位,上下按钮久按加速,兼容 ie,firefox,chrome。
调用方法:
$("#a").jscroll();
demo:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title>demo</title>
<link rel="stylesheet" type="text/css" href="/css/base.min.css" media="all"/>
<style type="text/css">
#a{width:500px;overflow:hidden;position:relative;height:200px;background:#f5f5f5;}
</style>
</head>
<body>
<div id="a">
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>底部</p>
</div>
</body>
</html>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.jscroll.js"></script>
<script type="text/javascript">
$(function(){
$("#a").jscroll();
});
</script>
高级应用(自定义滚动条背景及上下按钮):
调用图片:

demo:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title>demo</title>
<link rel="stylesheet" type="text/css" href="/css/base.min.css" media="all"/>
<style type="text/css">
#a{width:500px;overflow:hidden;position:relative;height:200px;background:#f5f5f5;}
</style>
</head>
<body>
<div id="a">
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>测试文字</p>
<p>底部</p>
</div>
</body>
</html>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.jscroll.js"></script>
<script type="text/javascript">
$(function(){
$("#a").jscroll({
W:"15px", //设置滚动条宽度
BgUrl:"url(/images/s_bg2.gif)", //设置滚动条背景图片地址
Bg:"right 0 repeat-y", //设置滚动条背景图片position,颜色等
Bar:{
Pos:"bottom", //设置滚动条初始化位置在底部
Bd:{ //设置滚动滚轴边框颜色:鼠标离开(默认),经过
Out:"#a3c3d5",
Hover:"#b7d5e6"
},
Bg:{ //设置滚动条滚轴背景:鼠标离开(默认),经过,点击
Out:"-45px 0 repeat-y",
Hover:"-58px 0 repeat-y",
Focus:"-71px 0 repeat-y"
}
},
Btn:{
btn:true, //是否显示上下按钮 false为不显示
uBg:{ //设置上按钮背景:鼠标离开(默认),经过,点击
Out:"0 0",
Hover:"-15px 0",
Focus:"-30px 0"
},
dBg:{ //设置下按钮背景:鼠标离开(默认),经过,点击
Out:"0 -15px",
Hover:"-15px -15px",
Focus:"-30px -15px"
}
},
Fn:function(){} //滚动时候触发的方法
});
});
</script>
jquery.jscroll.js:
/**
*
* Copyright (c) 2009 May(qq104010230)
* http://www.winwill.com
* http://www.winwill.com/jquery/jscroll.html
* admin@winwill.com
*/
/*--------------------------------------------------------------------------------------------------*/
$.fn.extend({//添加滚轮事件//by jun
mousewheel:function(Func){
return this.each(function(){
var _self = this;
_self.D = 0;//滚动方向
if($.browser.msie||$.browser.safari){
_self.onmousewheel=function(){_self.D = event.wheelDelta;event.returnValue = false;Func && Func.call(_self);};
}else{
_self.addEventListener("DOMMouseScroll",function(e){
_self.D = e.detail>0?-1:1;
e.preventDefault();
Func && Func.call(_self);
},false);
}
});
}
});
$.fn.extend({
jscroll:function(j){
return this.each(function(){
j = j || {}
j.Bar = j.Bar||{};//2级对象
j.Btn = j.Btn||{};//2级对象
j.Bar.Bg = j.Bar.Bg||{};//3级对象
j.Bar.Bd = j.Bar.Bd||{};//3级对象
j.Btn.uBg = j.Btn.uBg||{};//3级对象
j.Btn.dBg = j.Btn.dBg||{};//3级对象
var jun = { W:"15px"
,BgUrl:""
,Bg:"#efefef"
,Bar:{ Pos:"up"
,Bd:{Out:"#b5b5b5",Hover:"#ccc"}
,Bg:{Out:"#fff",Hover:"#fff",Focus:"orange"}}
,Btn:{ btn:true
,uBg:{Out:"#ccc",Hover:"#fff",Focus:"orange"}
,dBg:{Out:"#ccc",Hover:"#fff",Focus:"orange"}}
,Fn:function(){}}
j.W = j.W||jun.W;
j.BgUrl = j.BgUrl||jun.BgUrl;
j.Bg = j.Bg||jun.Bg;
j.Bar.Pos = j.Bar.Pos||jun.Bar.Pos;
j.Bar.Bd.Out = j.Bar.Bd.Out||jun.Bar.Bd.Out;
j.Bar.Bd.Hover = j.Bar.Bd.Hover||jun.Bar.Bd.Hover;
j.Bar.Bg.Out = j.Bar.Bg.Out||jun.Bar.Bg.Out;
j.Bar.Bg.Hover = j.Bar.Bg.Hover||jun.Bar.Bg.Hover;
j.Bar.Bg.Focus = j.Bar.Bg.Focus||jun.Bar.Bg.Focus;
j.Btn.btn = j.Btn.btn!=undefined?j.Btn.btn:jun.Btn.btn;
j.Btn.uBg.Out = j.Btn.uBg.Out||jun.Btn.uBg.Out;
j.Btn.uBg.Hover = j.Btn.uBg.Hover||jun.Btn.uBg.Hover;
j.Btn.uBg.Focus = j.Btn.uBg.Focus||jun.Btn.uBg.Focus;
j.Btn.dBg.Out = j.Btn.dBg.Out||jun.Btn.dBg.Out;
j.Btn.dBg.Hover = j.Btn.dBg.Hover||jun.Btn.dBg.Hover;
j.Btn.dBg.Focus = j.Btn.dBg.Focus||jun.Btn.dBg.Focus;
j.Fn = j.Fn||jun.Fn;
var _self = this;
var Stime,Sp=0,Isup=0;
$(_self).css({overflow:"hidden",position:"relative",padding:"0px"});
var dw = $(_self).width(), dh = $(_self).height()-1;
var sw = j.W ? parseInt(j.W) : 21;
var sl = dw - sw
var bw = j.Btn.btn==true ? sw : 0;
if($(_self).children(".jscroll-c").height()==null){//存在性检测
$(_self).wrapInner("<div class='jscroll-c' style='top:0px;z-index:9999;zoom:1;position:relative'></div>");
$(_self).children(".jscroll-c").prepend("<div style='height:0px;overflow:hidden'></div>");
$(_self).append("<div class='jscroll-e' unselectable='on' style=' height:100%;top:0px;right:0;-moz-user-select:none;position:absolute;overflow:hidden;z-inde

