首页 > 代码库 > slider(js)扩展
slider(js)扩展
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Slider图片轮播</title>
<style type="text/css">
*{margin:0;padding:0}
ul,li{list-style:none}
#slider{ position:relative;width:470px; height:150px; overflow:hidden;}
#img{ overflow:hidden;}
#img li{ float:left;width:468px; height:150px; }
#nav{ position:absolute; bottom:5px; right:5px;}
#nav li.nav{background:#ffb442;}
#nav li{border:1px solid #f47500; color:#d94b01; background:#fff;font-size:12px; height:15px;width:15px;float:left;margin-left:4px;display:inline;text-align:center}
</style>
<script type="text/javascript"><!--
var Hongru = {}, H$ = function(id){return document.getElementById(id)}, H$$ = function(c,p){return p.getElementsByTagName(c)}
Hongru.slider = function(){
return{
init:function(id,options){
var ul = this.u = H$$(‘ul‘,H$(id))[0], li = H$$(‘li‘,ul); this.l=li.length; this.index = 0;
if(options.navId&&options.curClass){this.nav = H$$(‘li‘,H$(options.navId)), this.c = options.curClass;}
this.a=options.auto||0; this.v=options.vertical||0;H$(id).style.overflow = ‘hidden‘;H$(id).style.position = ‘relative‘;ul.style.position=‘absolute‘;
if(this.v){ul.style.top=0; this.h=options.height||li[0].offsetHeight; ul.style.height=(this.l*this.h)+‘px‘;}
else{ul.style.left=0; this.w=options.width||li[0].offsetWidth; ul.style.width=(this.l*this.w)+‘px‘;}
this.pos(options.index||0,this.a?1:0);
},
pos:function(pos,a){
clearInterval(this.u.posAnim); clearInterval(this.u.auto);
var curPos=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left),
correctPos=this.v?pos*this.h:pos*this.w,
direction = correctPos>Math.abs(curPos)?1:-1;
correctPos*=-1;
this.index = pos;
if(this.nav){for(var i=0;i<this.l;i++){this.nav[i].className = i==pos?this.c:‘‘}}
this.u.posAnim = setInterval(function(){Hongru.slider.anim(correctPos,direction,a)},10);
},
anim:function(des,dir,a){
var curPos=this.v?parseInt(this.u.style.top):parseInt(this.u.style.left);
if(curPos == des){
clearInterval(this.u.posAnim);
if(a||this.a){Hongru.slider.auto()}
}
else{
var v=curPos-Math.ceil(Math.abs(des-curPos)*.07)*dir+‘px‘;
this.v?this.u.style.top=v:this.u.style.left=v;
}
},
auto:function(){
this.u.auto=setInterval(function(){Hongru.slider.move(1,1)},this.a*1000)
},
move:function(n,a){
var num=this.index+n, i=n==1?num==this.l?0:num:num<0?this.l-1:num; Hongru.slider.pos(i,a);
}
};
}();
// --></script>
</head>
<body>
<div id="slider">
<ul id="img">
<li><img src="http://images.cnblogs.com/cnblogs_com/hongru/262693/o_1.jpg" /></li>
<li><img src="http://images.cnblogs.com/cnblogs_com/hongru/262693/o_2.jpg" /></li>
<li><img src="http://images.cnblogs.com/cnblogs_com/hongru/262693/o_3.jpg" /></li>
</ul>
<ul id="nav">
<li onm ouseover="setTimeout(function(){Hongru.slider.pos(0)},300)">1</li>
<li onm ouseover="setTimeout(function(){Hongru.slider.pos(1)},300)">2</li>
<li onm ouseover="setTimeout(function(){Hongru.slider.pos(2)},300)">3</li>
</ul>
</div>
<script type="text/javascript">
Hongru.slider.init(‘slider‘,{
auto:3,
vertical:1,
navId:‘nav‘,
curClass:‘nav‘,
index:0});
</script>
</body>
</html>
slider(js)扩展