<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {margin: 0; padding: 0;}
.box {width: 490px; height: 170px; padding: 5px; border: 1px solid #ccc; margin: 200px auto;}
#inner {width: 490px; height: 170px; overflow: hidden; position: relative;}
ul {list-style: none; width: 600%; position: absolute; left: 0;}
li {float: left;}
.square {position: absolute; bottom: 10px; right: 10px;}
.square span {display: inline-block; width: 16px; height: 16px; margin: 3px; background-color: #fff; text-align: center; border: 1px solid #ccc; line-height: 16px; cursor: pointer;}
.square .current {background-color: darkorange; color: #fff;}
.arrow {display: none;}
.arrow span {position: absolute; top: 50%; margin-top: -23px; width: 20px; height: 46px; background-color: rgba(0,0,0,0.3); color: #FFFFFF; text-align: center; font: 400 15px/46px "consolas";}
.arrow span:hover {background-color: rgba(0,0,0,0.6);}
.arrow .arrow-left {left: 0; border-radius: 0 5px 5px 0;}
.arrow .arrow-right {right: 0; border-radius: 5px 0 0 5px;}
</style>
<script type="text/javascript">
window.onload = function () {
var inner = document.getElementById("inner");
var box = inner.parentNode;
var imgWidth = inner.offsetWidth;
var ul = inner.firstElementChild || inner.firstChild;
var square = inner.children[1];
var arrow = inner.children[2];
var spanArr = arrow.children;
var speed = 15;
for(var i=0; i<ul.children.length; i++){
var newSpan = document.createElement("span");
if(i==0){
newSpan.className = "current";
}
newSpan.innerHTML = i+1;
square.appendChild(newSpan);
}
var newLi = ul.children[0].cloneNode(true);
ul.appendChild(newLi);
var squareArr = square.children;
for(var i=0; i<square.children.length; i++){
squareArr[i].index = i;
squareArr[i].onmouseover = function(){
for(var j=0; j<squareArr.length; j++){
squareArr[j].className = "";
}
this.className = "current";
imgIndex = squareIndex = this.index;
animater(ul,-this.index*imgWidth,speed);
}
}
var timer = null;
var imgIndex = 0;
var squareIndex = 0;
timer = setInterval(autoPlay,1000);
function autoPlay() {
imgIndex++;
if(imgIndex>squareArr.length){
ul.style.left = 0;
imgIndex = 1;
}
animater(ul,-imgIndex*imgWidth,speed);
squareIndex++;
if(squareIndex>squareArr.length-1){
squareIndex = 0
}
for(var j=0; j<squareArr.length; j++){
squareArr[j].className = "";
}
squareArr[squareIndex].className = "current";
}
box.onmouseover = function () {
clearInterval(timer);
arrow.style.display = "block";
}
box.onmouseout = function () {
timer = setInterval(autoPlay,1000);
arrow.style.display = "none";
}
spanArr[0].onclick = function () {
imgIndex--;
if(imgIndex>squareArr.length){
ul.style.left = 0;
imgIndex = 1;
}else if(imgIndex<0){
ul.style.left = -(ul.children.length-1)*imgWidth+"px";
imgIndex = ul.children.length-2;
}
animater(ul,-imgIndex*imgWidth,speed);
squareIndex--;
if(squareIndex>squareArr.length-1){
squareIndex = 0
}else if(squareIndex<0){
squareIndex = squareArr.length-1;
}
for(var j=0; j<squareArr.length; j++){
squareArr[j].className = "";
}
squareArr[squareIndex].className = "current";
}
spanArr[1].onclick = function () {
autoPlay();
}
}
function animater (ele,target,speed) {
clearInterval(ele.timer);
speed = target>ele.offsetLeft ? speed : -speed;
ele.timer = setInterval(function(){
var val = ele.offsetLeft - target;
ele.style.left = ele.offsetLeft + speed + "px";
if(Math.abs(val) <= Math.abs(speed)){
ele.style.left = target + "px";
clearInterval(ele.timer);
}
},10);
}
</script>
</head>
<body>
<div class="box">
<div id="inner">
<ul>
<li>![](img/01.jpg)</li>
<li>![](img/02.jpg)</li>
<li>![](img/03.jpg)</li>
<li>![](img/04.jpg)</li>
<li>![](img/05.jpg)</li>
</ul>
<div class="square">
</div>
<div class="arrow">
<span class="arrow-left"><</span>
<span class="arrow-right">></span>
</div>
</div>
</div>
</body>
</html>
轮播图
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 网易轮播图: 案例素材及源码:链接:http://pan.baidu.com/s/1pLgzkVp 密码:9buw...
- 最近在学习Vue,然后做了一个轮播图,然后想起之前用jQuery做的轮播图,就打算进行一个对比。 jQuery轮播...