轮播图

<!DOCTYPE html>

<html lang="en">
 
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
border: 0;
}
 
.all {
width: 500px;
height: 200px;
padding: 7px;
border: 1px solid #ccc;
margin: 100px auto;
position: relative;
}
 
.screen {
width: 500px;
height: 200px;
overflow: hidden;
position: relative;
}
 
.screen li {
width: 500px;
height: 200px;
overflow: hidden;
float: left;
}
 
.screen ul {
position: absolute;
left: 0;
top: 0px;
width: 3000px;
}
 
.all ol {
position: absolute;
right: 10px;
bottom: 10px;
line-height: 20px;
text-align: center;
}
 
.all ol li {
float: left;
width: 20px;
height: 20px;
background: #fff;
border: 1px solid #ccc;
margin-left: 10px;
cursor: pointer;
}
 
.all ol li.current {
background: #DB192A;
}
 
#arr {
display: none;
}
 
#arr span {
width: 40px;
height: 40px;
position: absolute;
left: 5px;
top: 50%;
margin-top: -20px;
background: #000;
cursor: pointer;
line-height: 40px;
text-align: center;
font-weight: bold;
font-family: '黑体';
font-size: 30px;
color: #fff;
opacity: 0.3;
border: 1px solid #fff;
}
 
#arr #right {
right: 5px;
left: auto;
}
</style>
</head>
 
<body>
<div class="all" id='box'>
<!--相框-->
<div class="screen">
<ul>
<li>
<img src="images/41.jpg" width="500" height="200" />
</li>
<li>
<img src="images/42.jpg" width="500" height="200" />
</li>
<li>
<img src="images/43.jpg" width="500" height="200" />
</li>
<li>
<img src="images/44.jpg" width="500" height="200" />
</li>
<li>
<img src="images/45.jpg" width="500" height="200" />
</li>
</ul>
<ol>
</ol>
</div>
<div id="arr">
<span id="left">&lt;</span>
<span id="right">&gt;</span>
</div>
</div>
<!-- <script src="common.js"></script> -->
 
<script>
function my$(id) {
return document.getElementById(id);
}
 
function setInnerText(element, content) {
if (typeof element.textContent === "undefined") {
//IE浏览器
element.innerText = content;
} else {
element.textContent = content;
}
}
 
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(target - current) > Math.abs(step)) {
element.style.left = current + "px";
} else {
clearInterval(element.timeId);
element.style.left = target + "px";
}
}, 20);
}
//获取最外面的div
var box = my$("box");
//获取相框
var screen = box.children[0];
//获取相框的宽度
var imgWidth = screen.offsetWidth;
//获取ul
var ulObj = screen.children[0];
//获取ul中的li
var ulLiObj = ulObj.children;
//获取ol
var olObj = screen.children[1];
//获取的是左右焦点的div
var arr = my$("arr");
//获取左边的按钮
var left = my$("left");
//获取右边的按钮
var right = my$("right");
 
var pic = 0;
//页面加载后先创建小按钮,根据ul中的li个个数来创建li,把li加入到ol中
for (var i = 0; i < ulLiObj.length; i++) {
//创建li
var liObj = document.createElement("li");
//把li加入到ol中
olObj.appendChild(liObj);
setInnerText(liObj, (i + 1)); //兼容代码
//为每个ol中的li添加一个自定义属性存储索引值
liObj.setAttribute("index", i);
//为每个li注册鼠标进入事件
liObj.onmouseover = function () {
//先把ol中所有的li的背景颜色全部干掉
 
for (var j = 0; j < olObj.children.length; j++) {
olObj.children[j].removeAttribute("class");
}
//设置当前鼠标进入的li有背景颜色
this.className = "current";
//移动ul
//获取鼠标进入的ol中的li的索引值
pic = this.getAttribute("index");
animate(ulObj, -pic * imgWidth);
};
 
 
}
//第一个标签设置颜色
olObj.children[0].className = "current";
//追加一个图片到ul中
ulObj.appendChild(ulObj.children[0].cloneNode(true));
 
//页面加载之后自动移动
var timeId = setInterval(f1, 1000);
 
//鼠标进入
box.onmouseover = function () {
arr.style.display = "block";
clearInterval(timeId);
}
//鼠标离开
box.onmouseout = function () {
arr.style.display = "none";
timeId = setInterval(f1, 1000);
}
 
//右边焦点
right.onclick = f1;
 
function f1() {
//判断是否到达最后一张
if (pic == ulLiObj.length - 1) {
//跳转到第一张
pic = 0;
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");
}
//第一个按钮的颜色
if (pic == ulLiObj.length - 1) {
olObj.children[0].className = "current";
} else {
olObj.children[pic].className = "current";
}
};
 
//左边焦点
left.onclick = function () {
//判断是否到达第一张图片
if (pic == 0) {
//跳转到第六张图片
pic = ulLiObj.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";
}
</script>
 
</body>
 
</html>
posted @ 2019-08-31 15:16  石舟丿  阅读(132)  评论(0)    收藏  举报