HTML页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="viewpager.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>图片轮播练习</title>
<link href="CSS/viewpager.css" rel="stylesheet" />
</head>
<body>
<div class="box" id="box">
<div class="inner">
<ul>
<li><a href="#"><img src="image/a (1).jpg" alt="" /></a></li>
<li><a href="#"><img src="image/a (2).jpg" alt="" /></a></li>
<li><a href="#"><img src="image/a (3).jpg" alt="" /></a></li>
<li><a href="#"><img src="image/a (4).jpg" alt="" /></a></li>
</ul>
<ol class="bar">
</ol>
<div id="arr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
</div>
</body>
</html>
<script src="JavaScript/viewpaper1.js"></script>
CSS样式:
*{
margin:0;
padding:0;
}
.box{
width:640px;
height:480px;
border:1px solid #ccc;
margin:100px auto;
}
.inner{
width:640px;
height:480px;
position:relative;
overflow:hidden;
}
.inner img{
width:640px;
height:480px;
vertical-align:top;
}
ul{
width:1000%;
position:absolute;
list-style:none;
left:0;
top:0;
}
.inner li{
float:left;
}
ol{
position:absolute;
height:20px;
right:20px;
bottom:20px;
text-align:center;
padding:5px;
}
ol li{
display:inline-block;
width:20px;
height:20px;
line-height:20px;
background-color:#fff;
margin:5px;
cursor:pointer;
}
ol .current{
background-color: red;
}
#arr{
display:none;
}
#arr span{
width:40px;
height:40px;
position:absolute;
left:5px;
top:50%;
margin-top:-20px;
background:#fff;
cursor:pointer;
line-height:40px;
text-align:center;
font-weight:bold;
font-family:'黑体';
font-size:30px;
color:#000;
opacity:0.5;
border:1px solid #fff;
}
#arr #right{
right:5px;
left:auto;
}
JS代码:
function my$(id) {
return document.getElementById(id);
}
var box = my$("box");
var inner = box.children[0];
var ulObj = inner.children[0];
var list = ulObj.children;
var olObj = inner.children[1];
var arr = my$("arr");
var imgWidth = inner.offsetWidth;
var right = my$("right");
var pic = 0;
for (var i = 0; i < list.length; i++) {
var liObj = document.createElement("li");
olObj.appendChild(liObj);
liObj.innerText = (i + 1);
liObj.setAttribute("index", i);
liObj.onclick = function () {
for (var j = 0; j < olObj.children.length; j++) {
olObj.children[j].removeAttribute("class");
}
this.className = "current";
pic = this.getAttribute("index");
animate(ulObj, -pic * imgWidth);
};
}
olObj.children[0].className = "current";
ulObj.appendChild(ulObj.children[0].cloneNode(true));
var timeId = setInterval(onmouseclickHandle, 1500);
box.onmouseover = function () {
arr.style.display = "block";
clearInterval(timeId);
};
box.onmouseout = function () {
arr.style.display = "none";
timeId = setInterval(onmouseclickHandle, 1500);
};
right.onclick = onmouseclickHandle;
function onmouseclickHandle() {
if (pic == list.length - 1) {
pic = 0;
ulObj.style.left = 0 + "px";
}
pic++;
animate(ulObj, -pic * imgWidth);
if (pic == list.length - 1) {
olObj.children[olObj.children.length - 1].className = "";
olObj.children[0].className = "current";
}else{
for(var i=0;i<olObj.children.length;i++){
olObj.children[i].removeAttribute("class");
}
olObj.children[pic].className = "current";
}
}
left.onclick = function () {
if (pic == 0) {
pic = list.length - 1;
ulObj.style.left = -pic * imgWidth + "px";
}
pic--;
animate(ulObj, -pic * imgWidth);
for (var i = 0; i < olObj.children.length; i++) {
olObj.children[i].removeAttribute("class");
}
olObj.children[pic].className = "current";
};
function animate(element, target) {
clearInterval(element.timeId);
element.timeId = setInterval(function () {
var current = element.offsetLeft;
var step = 10;
step = current < target ? step : -step;
current += step;
if (Math.abs(current - target) > Math.abs(step)) {
element.style.left = current + "px";
}
else {
clearInterval(element.timeId);
element.style.left = target + "px";
}
}, 10);
}