JavaScript 缓动焦点图实现的几种写法 封装 + 函数式 + JQuery
2012-03-28 11:58 VVG 阅读(2494) 评论(0) 编辑 收藏 举报焦点图
原生JavaScript封装:
调用方式:
<script type="text/javascript">
vvg.slider.init('focus_pic'/**包裹图片的UL的ID**/, {
v:1,//1表示垂直上下移动幻灯片 0表示左右移动幻灯片
height:180,//幻灯片高度
width:530,//幻灯片宽度
btnId:"focus_btn",//幻灯片按钮的ID
autoTime:1000//自动播放间隔时间
});
</script>
封装源代码+注释:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>焦点图</title>
<style type="text/css">
#focus_box{ position:relative; margin:0 auto; overflow:hidden; zoom:1;}
#focus_box ul{ list-style:none; padding:0; margin:0;}
#focus_btn{ position:absolute; right:5px; bottom:5px; z-index:2;}
#focus_pic{ position:absolute;}
#focus_pic li{ margin:0; padding:0; float:left;}
#focus_pic li img{ border:none;float:left;}
#focus_btn li{ float:left; font-size:12px; width:25px; height:25px; line-height:25px; font-weight:bold; text-align:center; background:#fff; color:#000; margin-right:2px; cursor:pointer; border-radius:20px;}
#focus_btn li.on{ background:#f60; color:#fff;}
</style>
</head>
<body>
<div id="focus_box" >
<ul id="focus_pic" style="left:0;top:0;">
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
</ul>
</div>
<script type="text/javascript">
var vvg = {};
var getId = function(id){
return document.getElementById(id);
}
var getChild = function(child,parent){
return parent.getElementsByTagName(child);
}
vvg.slider = function(){
return {
init:function(id,oo){
var warper = this.warper = getId(id);//获取包裹图片DOM(第一个UL的ID)
var warpLis = getChild("li",warper);//获取包裹下的LI子元素
var liNo = this.no = warpLis.length;//获取包裹LI元素的个数
var height = this.height = oo.height || warpLis[0].offsetHeight; //获取图片高度
var width = this.width = oo.width || warpLis[0].offsetWidth; //获取图片宽度
this.index = 0;//自动播放从第几个开始
this.autoTime = oo.autoTime || 2000; //自动播放间隔时间
this.btnId = oo.btnId || "focus_btn"; //图片上面的数字按钮的ID
this.v = oo.v || 0; //垂直移动OR水平移动
warper.parentNode.style.width = this.width + "px";//按照指定的宽度设置父标签的宽度
warper.parentNode.style.height = this.height + "px";//设置父标签的高度
if(!!this.v){//判断时候是垂直播放,设置相应的宽度
warper.style.width = width + "px";
}else{
warper.style.width = width*liNo + "px";
}
this.makeBtn(liNo,this.btnId);//调用makeBtn生成数字按钮,focus_btn 为按钮UL的ID,可以用这个ID设置小按钮的CSS样式
this.btnLis = document.getElementById(this.btnId).getElementsByTagName("li"); //获得生成的按钮LI NODELIST
this.btnLis[0].className = "on";//按钮初始化CLASS
this.autoPlay();//自动播放
this.btnHover();//按钮效果
},
makeBtn:function(no,c){//生成按钮 no表示个数 C表示UL的ID
var btnUl = document.createElement("ul");
btnUl.id = c;
var strLi = "";
for(var i=0;i<no;i++){
strLi = strLi + "<li>" + (i + 1) + "</li>";
}
btnUl.innerHTML = strLi;
this.warper.parentNode.appendChild(btnUl);//添加到父div下
},
autoPlay:function(){//自动播放
this.moveIndex(this.index);
for(var i=0;i<this.no;i++){
this.btnLis[i].className = "";
}
this.btnLis[this.index].className = "on";//设置当前Btn的CLASS
},
moveIndex:function(index){//传递需要移动的INDEX索引值
var posx = this.v?0:-index*this.width; //计算需要移动的LEFT位置
var posy = this.v?-index*this.height:0;//计算需要移动的TOP位置
this.Timer = setInterval(function(){vvg.slider.moveTo(posx,posy);},10); //间歇调用MOVETO 移动到目标位置
},
moveTo:function(posx,posy){//移动
var left = parseInt(this.warper.style.left,10);
var top = parseInt(this.warper.style.top,10);
if(left == posx && top == posy){//当移动到目标位置时
clearInterval(this.Timer); //清除间歇调用
this.index++; //index加1
if(this.index==this.no){//到达最后后设置INDEX为0
this.index = 0;
}
this.autoTimer = setTimeout(function(){vvg.slider.autoPlay();},this.autoTime);//开始播放下一个图片
return false;
}
left = left>posx?left-Math.ceil((left-posx)/10):left+ Math.ceil((posx-left)/10);//当目标位置大于当前位置的时候,一次移动LEFT坐标
top = top>posy?top-Math.ceil((top-posy)/10):top+Math.ceil((posy-top)/10); //同上
this.warper.style.left = left + "px";
this.warper.style.top = top + "px";
},
btnHover:function(){//设置按钮的鼠标事件
for(var i=0; i<this.no;i++){
this.btnLis[i].onmouseover = function(t){
return function(){
if(vvg.slider.autoTimer){clearInterval(vvg.slider.autoTimer)};//清除播放
if(vvg.slider.Timer){clearInterval(vvg.slider.Timer)};
vvg.slider.index = t;
vvg.slider.autoPlay();//移动到this.index
}
}(i)
}
}
}
}();
</script>
<script type="text/javascript">
vvg.slider.init('focus_pic'/**包裹图片的UL的ID**/, {
v:1,//1表示垂直上下移动幻灯片 0表示左右移动幻灯片
height:180,//幻灯片高度
width:530,//幻灯片宽度
btnId:"focus_btn",//幻灯片按钮的ID
autoTime:1000//自动播放间隔时间
});
</script>
</body>
</html>
原生函数式代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>焦点图</title>
<style type="text/css">
#focus_box{ width:530px; height:180px; position:relative; margin:0 auto; overflow:hidden; zoom:1;}
#focus_box ul{ list-style:none; padding:0; margin:0;}
#focus_btn{ position:absolute; right:5px; bottom:5px; z-index:2;}
#focus_pic{ position:absolute;}
#focus_pic li{ height:180px; width:530px; margin:0; padding:0; float:left}
#focus_btn li{ float:left; font-size:12px; width:25px; height:25px; line-height:25px; font-weight:bold; text-align:center; background:#fff; color:#000; margin-right:2px; cursor:pointer;}
#focus_btn li.on{ background:#f60; color:#fff;}
img{border:none; margin:0;padding:0; disaplay:block;float:left}
</style>
</head>
<body>
<div id="focus_box" >
<ul id="focus_pic" style="left:0;top:0;">
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
</ul>
<ul id="focus_btn">
<li class="">1</li>
<li class="">2</li>
<li class="">3</li>
<li class="">4</li>
<li class="">5</li>
<li class="">6</li>
</ul>
</div>
<script type="text/javascript">
(function(){
var picHeight = 180;//幻灯片的高度
var picWidth = 530;//幻灯片的宽度
var autoTime = 1000;//自动播放间隔时间
var index = 0; //从第几个开始轮播,0为第一个
var v = 1;//1为垂直播放,0为水平播放
var focusPics = document.getElementById("focus_pic");//获得幻灯片的框架ID
var focusLi = focusPics.getElementsByTagName("li");
var picNo = focusLi.length;//幻灯片的数量
var Timer,autoTimer;
var posX,posY;//移动的目标距离
var focus_btn = document.getElementById("focus_btn");
var btnLi = focus_btn.getElementsByTagName("li");
btnLi[0].className = "on";
if(v==0){//当方向为水平移动时候
focusPics.style.width = picNo*picWidth + "px"; //设置包裹UL的宽度为LI的数量乘以图片的宽度
for(var i=0;i<picNo;i++){//设置样式
focusLi[i].style.float = "left";
focusLi[i].style.height = picHeight + "px";
focusLi[i].style.widht = picWidth + "px";
}
}else if(v==1){ //垂直方向
for(var i=0;i<picNo;i++){
focusPics.style.width = picWidth + "px";
}
}
function autoPlay(){//自动播放函数
if(autoTimer){clearInterval(autoTimer)}; //清除播放避免重复播放
Timer = setInterval(function(){move(index)} //调用移动函数,传入index值
,autoTime);
}
function move(liIndex){ //移动
if(v == 1){//垂直移动时
posX = 0;
posY = -(liIndex*picHeight);//取得垂直移动的目标TOP值
}else{//水平移动时
posX = -(liIndex*picWidth);//取得水平移动的目标LEFT值
posY = 0;
}
for(var i=0;i<picNo;i++){
btnLi[i].className = "";//清除按钮的样式
}
btnLi[liIndex].className = "on";//设置按钮的样式
autoTimer = setInterval( function(){moveElement(posX,posY)},10); //调用缓冲移动到目标位置函数
}
function moveElement(posX,posY){ //posX posY 为移动到的目标坐标
if(Timer){clearInterval(Timer)}; //移动的时候清除自动播放
//if(autoTimer){clearInterval(autoTimer)};
var stepx,stepy;
if(!focusPics.style.left) focusPics.style.left = "0px"; //初始化
if(!focusPics.style.top) focusPics.style.top = "0px"; //初始化
var left = focusPics.style.left;
var top = focusPics.style.top;
left = parseInt(left,10);
top = parseInt(top,10);
if(left == posX && top==posY){//当到达目标位置时
if(autoTimer){clearInterval(autoTimer)}; //清除缓冲移动函数的间隙调用
Timer = setInterval( //自动播放下一个位置
function(){
move(index);
index++;//索引加一
if(index==picNo){//当索引值等于LI的数量时
index = 0; //返回到第一个
}
}
,autoTime);
return false;
};
if(left>posX){//判断移动的位置与现在的位置的大小
stepx = Math.ceil((left-posX)/10);
left = left - stepx;
}else if(left<posX){
stepx = Math.ceil((posX-left)/10);
left = left + stepx;
}
if(top>posY){
stepy = Math.ceil((top-posY)/10);
top = top - stepy;
}else if(top<posY){
stepy = Math.ceil((posY - top)/10);
top = top + stepy;
}
focusPics.style.left = left + "px";
focusPics.style.top = top + "px";
}
function btnHover(){//小按钮的鼠标效果
var l = btnLi.length;
for(var i=0; i<l;i++){
btnLi[i].onmouseover = function(t){
return function(){
if(autoTimer){clearInterval(autoTimer)};//清除播放后
index = t;
move(t);//移动到this.index
}
}(i)
}
}
autoPlay();
btnHover();
})()
</script>
</body>
</html>
JQuery实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>焦点图</title>
<style type="text/css">
#focus_box{ width:530px; height:180px; position:relative; margin:0 auto; overflow:hidden;}
#focus_box ul{ list-style:none; padding:0; margin:0;}
#focus_btn{ position:absolute; right:5px; bottom:5px; z-index:2;}
#focus_pic{ position:absolute; top:-180px;}
#focus_pic li{ height:180px; width:530px; margin:0; padding:0; float:left;}
#focus_pic li img{float:left; border:none;}
#focus_btn li{ float:left; font-size:12px; width:25px; height:25px; line-height:25px; font-weight:bold; text-align:center; background:#fff; color:#000; margin-right:2px; cursor:pointer;}
#focus_btn li.on{ background:#f60; color:#fff;}
</style>
</head>
<body>
<div id="focus_box" >
<ul id="focus_pic" style="left:0px; top:0px;">
<li><a target="_blank" href="#"><img id="sa_fp_hd_1_img" alt="" src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img id="sa_fp_hd_1_img" alt="" src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img id="sa_fp_hd_1_img" alt="" src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111201/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img id="sa_fp_hd_1_img" alt="" src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111202/530_180.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="http://img.fqmall.cn/WebResources/boc/Xdv/shopping/images/20111205/530_180.jpg" width="530" height="180"></a></li>
</ul>
<ul id="focus_btn">
<li class="on">1</li>
<li class="">2</li>
<li class="">3</li>
<li class="">4</li>
<li class="">5</li>
<li class="">6</li>
</ul>
</div>
<script src="Jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){
var timer,
index = 0,
liNum = $("#focus_pic li").length,//总数量
onceMove = 180,//一次移动数量
maxTop = (liNum-1)*onceMove,//TOP移动最大值
speed = 300,//移动速度
autoTime = 1000;//自动播放间隔
function move(){
var nowTop = Math.abs(parseInt($("#focus_pic").css("top")));
if(nowTop == maxTop){
$("#focus_pic").stop().animate({top:"0"},speed);
index = 0;
}else{
$("#focus_pic").stop().animate({top:"-="+onceMove+"px"},speed);
index ++;
};
$("#focus_btn li").eq(index).addClass("on").siblings().removeClass("on");
}
timer = setInterval(move,autoTime);
$("#focus_btn li").mouseover(function(){
if(timer){ clearInterval(timer);}
$(this).addClass("on").siblings().removeClass("on");
index = $("#focus_btn li").index(this);
$("#focus_pic").stop().animate({top:"-" + onceMove*index + "px"},speed);
}).mouseleave(function(){
timer = setInterval(move,autoTime);
})
$("#focus_pic li").mouseover(function(){
if(timer){ clearInterval(timer);}
}).mouseleave(function(){
timer = setInterval(move,autoTime);
})
})
</script>
</body>
</html>
转载请注明出处:http://www.cnblogs.com/NNUF/