JavaScript缓动焦点图实现的几种写法:封装+函数式+JQuery
焦点图
原生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="/upload/2013111520/20120515095112934.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="/upload/2013111520/20120515095113176.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="/upload/2013111520/20120515095115916.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="/upload/2013111520/20120515095112934.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="/upload/2013111520/20120515095113176.jpg" width="530" height="180"></a></li>
<li><a target="_blank" href="#"><img src="/upload/2013111520/20120515095115916.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>";
补充:web前端 , JavaScript ,