一个简单的图片轮播案例
<head>
<title>图片轮播</title>
<meta charset="utf-8" />
<style type="text/css">
.box1 {
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 100px;
left: 200px;
}
.box2 {
width: 100px;
height: 100px;
background: green;
position: absolute;
top: 50px;
left: 350px;
}
.box3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 100px;
left: 500px;
}
</style>
</head>
<body>
<script>
window.onload = function(){
var oDiv = document.getElementsByTagName('div');
var oInput = document.getElementsByTagName('input');
var arr = [];
for (var i=0; i<oDiv.length; i++) {
arr.push([getStyle(oDiv[i],'top'), getStyle(oDiv[i],'left') ]);//复合数组
};
// console.log(arr);
//点击第一个按钮
oInput[0].onclick = function(){
arr.push(arr[0]); //尾部添加
arr.shift(); //头部删除
for (var i=0; i<oDiv.length; i++) {
oDiv[i].style.top = arr[i][0];
oDiv[i].style.left = arr[i][1];
};
};
//点击第二个按钮
oInput[1].onclick = function(){
arr.unshift(arr[oDiv.length-1]); //头部添加
arr.pop(); //尾部删除
for (var i=0; i<oDiv.length; i++) {
oDiv[i].style.top = arr[i][0];
oDiv[i].style.left = arr[i][1];
};
};
//获取style属性,通过封装成一个函数来获取
function getStyle(obj, attr){
if (obj.currentStyle) { //IE识别currentStyle
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj, false)[attr];//非IE识别getComputedStyle(obj,false)
};
};
};
</script>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
<input type="button" value="<" />
<input type="button" value=">" />
</body>
<title>图片轮播</title>
<meta charset="utf-8" />
<style type="text/css">
.box1 {
width: 100px;
height: 100px;
background: red;
position: absolute;
top: 100px;
left: 200px;
}
.box2 {
width: 100px;
height: 100px;
background: green;
position: absolute;
top: 50px;
left: 350px;
}
.box3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
top: 100px;
left: 500px;
}
</style>
</head>
<body>
<script>
window.onload = function(){
var oDiv = document.getElementsByTagName('div');
var oInput = document.getElementsByTagName('input');
var arr = [];
for (var i=0; i<oDiv.length; i++) {
arr.push([getStyle(oDiv[i],'top'), getStyle(oDiv[i],'left') ]);//复合数组
};
// console.log(arr);
//点击第一个按钮
oInput[0].onclick = function(){
arr.push(arr[0]); //尾部添加
arr.shift(); //头部删除
for (var i=0; i<oDiv.length; i++) {
oDiv[i].style.top = arr[i][0];
oDiv[i].style.left = arr[i][1];
};
};
//点击第二个按钮
oInput[1].onclick = function(){
arr.unshift(arr[oDiv.length-1]); //头部添加
arr.pop(); //尾部删除
for (var i=0; i<oDiv.length; i++) {
oDiv[i].style.top = arr[i][0];
oDiv[i].style.left = arr[i][1];
};
};
//获取style属性,通过封装成一个函数来获取
function getStyle(obj, attr){
if (obj.currentStyle) { //IE识别currentStyle
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj, false)[attr];//非IE识别getComputedStyle(obj,false)
};
};
};
</script>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
<input type="button" value="<" />
<input type="button" value=">" />
</body>