原生javascript-焦点图待优化

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>焦点轮播图</title>
<style type="text/css">
*{ margin: 0; padding: 0; text-decoration: none;}
body { padding: 20px;}
#container { width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; position: relative;}
#list { width: 4200px; height: 400px; position: absolute; z-index: 1;}
#list img { float: left;width:600px;display: block;}
#buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}
#buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}
#buttons .on { background: orangered;}
.arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px; position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
.arrow:hover { background-color: RGBA(0,0,0,.7);}
#container:hover .arrow { display: block;}
#prev { left: 20px;}
#next { right: 20px;}
</style>
</head>
<body>

<div id="container">
<div id="list" style="left: -600px;">
<img src="http://img1.funshion.com/pictures/pocket/201406/4313305aeba.jpg" width="600" height="400" alt="1"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/5cb19103b05.jpg" width="600" height="400" alt="1"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/4f67b1026bc.jpg" width="600" height="400" alt="2"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/a1e2e9a196f.jpg" width="600" height="400" alt="3"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/720eff97cdb.jpg" width="600" height="400" alt="4"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/4313305aeba.jpg" width="600" height="400" alt="5"/>
<img src="http://img1.funshion.com/pictures/pocket/201406/5cb19103b05.jpg" width="600" height="400" alt="5"/>
</div>
<div id="buttons">
<span index="1" class="on"></span>
<span index="2"></span>
<span index="3"></span>
<span index="4"></span>
<span index="5"></span>
</div>
<a href="javascript:;" id="prev" class="arrow"><</a>
<a href="javascript:;" id="next" class="arrow">></a>
</div>
</body>
<script type="text/javascript">
window.onload = function(){
var container = document.getElementById('container');
var list = document.getElementById('list');
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var width = parseInt(list.getElementsByTagName('img')[0].offsetWidth, 10);
var num = 5;
var index = 1;
var intervalTime = 3000;
var autoTimer = null;
var goTimer = 0;

function showBtn(){
    for(var i = 0, l = buttons.length; i < l; i++){
        if(buttons[i].className == 'on'){
            buttons[i].className = '';
            break;
        }
    }
    buttons[index - 1].className = 'on';
}

function animation(offWidth){
    clearTimeout(goTimer);
    //运动效果
    var listLeft = parseInt(list.style.left, 10);
    var timer = 300, ttl = 30, speed = (offWidth - listLeft)/ (timer/ttl);

    function go(){
        list.style.left = (speed > 0 ? Math.min : Math.max)(parseInt(list.style.left) + speed, offWidth) + 'px';
        goTimer = setTimeout(go, ttl);

        if(parseInt(list.style.left, 10) == offWidth){
          clearTimeout(goTimer);
        }
    }
    go();
}

function autoPlay(){
    autoTimer = setTimeout(function(){
        next.onclick();
        autoPlay();
    }, intervalTime);
}

function stopPlay(){
    clearTimeout(autoTimer);
}

next.onclick = function(){
    if(index >= num){
        index = 1;
    }else{
       index++;
    }
    showBtn();
    animation(index*(-width));
};

prev.onclick = function(){
    if(index <= 1){
       index = 5;
    }else{
        index--;
    }
    showBtn();
    animation(index*(-width));
};

for(var i = 0, l = buttons.length; i < l; i++){
    buttons[i].onclick = function(){
        if(this.className == 'on'){//如果当前是选中状态 点击退出
           return;
        }

        var myIndex = parseInt(this.getAttribute('index'));
        var offset = myIndex * (-width);
        animation(offset);
        index = myIndex;//重新给index赋值
        showBtn();
    };
}
autoPlay();
container.onmouseover = stopPlay;
container.onmouseout = autoPlay;
}
</script>
</html>

posted @ 2014-12-03 13:14  sunhw360  阅读(97)  评论(0编辑  收藏  举报