圖片輪播

<!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=utf-8" />
<title>CSS+JS圖片輪播切換效果原生js面向對象封裝版丨芯晴網頁特效丨CsrCode.Cn</title>
<style>
ul li{
list-style: none;
}
#box {
position:relative; width:480px; height:300px;}
#box .imgList{ position:relative; width:480px; height:300px; overflow:hidden;}
#box .imgList li{ position:absolute; top:0; left:0; width:480px; height:300px;}
#box .countNum{ position:absolute; right:0; bottom:5px; z-index: 999;}
#box .countNum li{ width:20px; height:20px; float:left; color:#fff; border-radius:20px; background:#f90; text-align:center;
margin-right:5px; cursor:pointer; opacity:0.7; filter:alpha(opacity=70);}
#box .countNum li.current{ background:#f60; font-weight:bold; opacity:1; filter:alpha(opacity=70);}html

.innerText { height: 50px; font:17.93px/30px"microsoft yahei"; width:480px;line-height: 50px; z-index:100;text-align: left;position: absolute;top: 250px;opacity: 0.4;color: #fff;background: rgb(76,76,76); }
.innerText1 { height: 50px; font:17.93px/30px"microsoft yahei"; width:480px;line-height: 50px;text-align: left;position: absolute;top: 250px;color: #fff;z-index: 998;text-decoration: none;}
.innerText1 a{ text-decoration: none; color: #fff;}
.innerText1 a:hover{ color: #f00; text-decoration: none;}
.innerText1 a :visited{ color: #fff; text-decoration: none;}
-->
</style>app

<script>
<!--
function runImg(){}
runImg.prototype={
bigbox:null,//最外層容器
boxul:null,//子容器ul
imglist:null,//子容器img
numlist:null,//子容器countNum
index:0,//當前顯示項
timer:null,//控制圖片轉變效果
play:null,//控制自動播放
imgurl:[],//存放圖片
count:0,//存放的個數
spanText:[],
$:function(obj){
if(typeof(obj)=="string"){
if(obj.indexOf("#")>=0){
obj=obj.replace("#","");
if(document.getElementById(obj)){
return document.getElementById(obj);
} else{alert("沒有容器"+obj);
return null;
}
}else{
return document.createElement(obj);
}
}else{ return obj;}
},
//初始化
info:function(id){
this.count=this.count<=6?this.count:6;
this.bigbox=this.$(id);
for(var i=0;i<2;i++){
var ul=this.$("ul");
for(var j=1;j<=this.count;j++){
var li=this.$("li");
li.innerHTML=i==0?this.imgurl[j-1]:j;
var innerTexts=document.getElementsByClassName('innerText');
console.log(innerTexts[0]);
ul.appendChild(li);
}
this.bigbox.appendChild(ul);
}
this.boxul=this.bigbox.getElementsByTagName("ul");
this.boxul[0].className="imgList";
this.boxul[1].className="countNum";
this.imglist=this.boxul[0].getElementsByTagName("li");
this.numlist=this.boxul[1].getElementsByTagName("li");
this.numlist[0].className="current";
},
//封裝程序入口
action:function(id){
this.autoplay();
this.mouseoverout(this.bigbox,this.numlist);
},
//圖片切換效果
imgshow:function(num,numlist,imglist){
this.index=num;
var innerTexts=document.getElementsByClassName('innerText1');
spanText=["<a href=\"http://taizhou.19lou.com/forum-1635-thread-163421419813606643-1-1.html\">這輛是mini嗎 表騙我</a>",
"<a href=\"http://taizhou.19lou.com/forum-1635-thread-163451420010155737-1-1.html\">服來戰 男女司機誰最坑</a>",
"<a href=\"http://taizhou.19lou.com/forum-1635-thread-163331420528103083-1-1.html\">你們來找茬</a>",
"<a href=\"http://taizhou.19lou.com/forum-830-thread-163841420695084451-1-1.html\">豪定製的邁倫凱</a>",
"<a href=\"##\">第5輛車</a>",
"<a href=\"##\">第6輛車</a>"];
var a=spanText[num];
innerTexts[0].innerHTML=a;測試

var alpha=0;
for(var i=0;i<numlist.length;i++){
numlist[i].className="";
}
numlist[this.index].className="current";ui

clearInterval(this.timer);
for(var j=0;j<imglist.length;j++){
imglist[j].style.opacity=0;
imglist[j].style.filter="alpha(opacity=100)";
}
var $this=this;
//利用透明度來實現切換圖片
this.timer=setInterval(function(){
alpha+=2;
if(alpha>100){alpha=100};//不能大於100
//爲兼容性賦樣式
imglist[$this.index].style.opacity=alpha/100;
imglist[$this.index].style.filter="alpha(opacity="+alpha+")";
if(alpha==100){clearInterval($this.timer)};//當等於100的時候就切換完成了
},20)//經測試20是我認爲最合適的值
},
//自動播放
autoplay:function(){
var $this=this;
this.play=setInterval(function(){
$this.index++;
if($this.index>$this.imglist.length-1){$this.index=0};
$this.imgshow($this.index,$this.numlist,$this.imglist);
},3000)
},
//處理鼠標事件
mouseoverout:function(box,numlist){
var $this=this;
box.onmouseover=function(){
clearInterval($this.play);
}
box.onmouseout=function(){
$this.autoplay($this.index);
}
for(var i=0;i<numlist.length;i++){
numlist[i].index=i;
numlist[i].onmouseover=function(){
$this.imgshow(this.index,$this.numlist,$this.imglist);
}
}
}
}
window.onload=function(){
var runimg=new runImg();
runimg.count=6;
runimg.imgurl=[
"<img src=\"http://i.mmcdn.cn/simba/img/T117eTXmXqXXXXXXXX.jpg\"/>",
"<img src=\"http://img03.taobaocdn.com/tps/i3/T1t8eTXbBtXXXXXXXX-490-170.png\"/>",
"<img src=\"http://i.mmcdn.cn/simba/img/T1OVOUXeNjXXXXXXXX.jpg\"/>",
"<img src=\"http://i.mmcdn.cn/simba/img/T1J.9TXc8lXXXXXXXX.jpg\"/>",
"<img src=\"http://i.mmcdn.cn/simba/img/T1J.9TXc8lXXXXXXXX.jpg\"/>",
"<img src=\"http://img03.taobaocdn.com/tps/i3/T1ITuTXbRnXXXXXXXX-490-170.png\"/>"];this

runimg.info("#box");
runimg.action("#box");
}
-->
</script>
</head>
<body>url

<div id="box">
<div class="innerText"> </div>
<span class="innerText1">第一輛車</span>
</div>

</body>
</html>spa

相關文章
相關標籤/搜索