仿淘宝幻灯片,基础版,后期效果是要做到每次点击小圆点,切换都无缝
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4.淘宝幻灯片</title>
<meta name="author" content="Administrator" />
<!-- Date: 2014-12-11 -->
<style>
*{margin:0;padding:0}
#div1{width:400px;height:215px; border:1px solid red;margin:40px auto; overflow:hidden;position:relative }
ul{position:absolute;left:0;top:0}
li{list-style:none;float:left; position: relative}
li.active{background:red}
li.active2{border:1px solid red}
li img{width:400px;}
ol{position:absolute}
ol li{width:15px;height:15px;border-radius:10px;background:#808080;margin:0 3px}
.t{position:absolute;font-size:80px;z-index:2;left:200px;top:100px;color:red;width:100px;height:100px;background:#fff;text-align:center; border:1px solid black}
</style>
<script>
window.onload=function(){
var oDiv1=document.getElementById('div1');
var oUl=oDiv1.getElementsByTagName('ul')[0];
var aLi=oUl.getElementsByTagName('li');
var arr=['3-images/1.jpg','3-images/2.jpg','3-images/3.jpg','3-images/4.jpg','3-images/5.jpg'];
var arr1=['1','2','3','4','5'];
oUl.style.width = aLi.length * aLi[0].offsetWidth +'px'; var oL=document.createElement('ol');
var str='';
for( var i=0;i<aLi.length;i++ ){ str += '<li></li>'
}
oL.innerHTML=str;
oDiv1.appendChild( oL );
document.title = oDiv1.offsetWidth +'-'+oL.offsetWidth;
oL.style.left = (oDiv1.offsetWidth - oL.offsetWidth)/2 +'px';
oL.style.bottom='0px';
var aLi1=oL.getElementsByTagName('li'); var timer=null;
var num=0; function init(){
for(var i=0;i<aLi1.length;i++){
aLi1[i].className=''
}
aLi1[num].className='active'
}
init();
function slide(){
num++;
if(num>=aLi.length) {
num=0;
oUl.style.left = 0;
}
init();
hcMove(oUl,{
'left':-400*num
})
}
timer=setInterval(function(){
slide()
},1000) oDiv1.onmouseover=function(){
clearInterval(timer)
}
oDiv1.onmouseout=function(){
timer=setInterval(function(){
slide()
},1000)
}
for(var i=0;i<aLi.length;i++){
aLi1[i].index=i;
aLi1[i].onclick=function(){
num = this.index;
init();
hcMove(oUl,{'left':-400*num})
}
} }
function css(obj,attr) {
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr]
}
/**完美 缓冲运动 带opacity**/
function hcMove(obj,json,fn){
clearInterval( obj.timer ); obj.timer=setInterval(function(){
var iBtn=true; for(var attr in json){
var target=json[attr]; if( attr == 'opacity' ){
var iSpeed = (target-Math.round(css(obj,attr))*100)/6;
}else{
var iSpeed = (target-parseInt(css(obj,attr)))/6;
} iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);//速度取整,因为js中的小数是经过计算的,默认四舍五入,但是不够0.5的就不会动了 if( parseInt(css(obj,attr)) != target ){
iBtn=false;//如果有运动没到达目标点,iBtn为false if( attr == 'opacity' ){
var sNow=Math.round(css(obj,attr)*100) + iSpeed; //先做处理 再赋值
if( sNow > target && iSpeed >0 || sNow < target && iSpeed <0 ){
sNow = target
} obj.style[attr] = sNow/100;
obj.style.filter = 'alpha(opacity='+sNow+')';
}else{ var sNow = parseInt(css(obj,attr)) + iSpeed; //先做处理 再赋值
if( sNow > target && iSpeed >0 || sNow < target && iSpeed <0 ){
sNow = target
}
obj.style[attr] = sNow +'px';
} } } if(iBtn){ //如果运动全部完成,iBtn为true
clearInterval(obj.timer);
fn && fn()
} },30)
} </script>
</head>
<body>
<div id="div1">
<ul>
<li mars='1'><img src="3-images/1.jpg"><span class="t">1</span></li>
<li mars='2'><img src="3-images/2.jpg"><span class="t">2</span></li>
<li mars='3'><img src="3-images/3.jpg"><span class="t">3</span></li>
<li mars='4'><img src="3-images/4.jpg"><span class="t">4</span></li>
<li mars='5'><img src="3-images/5.jpg"><span class="t">5</span></li>
</ul>
</div>
</body>
</html>