如何在静态页面中用JS制作页面滚动图片广告
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="common.js"></script>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
border: 0;
}
.all {
width: 500px;
height: 200px;
cursor: pointer;
/* 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: yellow;
}
#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/1.jpg" width="500" height="200" /></li>
<li><img src="images/2.jpg" width="500" height="200" /></li>
<li><img src="images/3.jpg" width="500" height="200" /></li>
<li><img src="images/4.jpg" width="500" height="200" /></li>
<li><img src="images/5.jpg" width="500" height="200" /></li>
</ul>
<ol></ol>
<div id="arr"><span id="left"><</span><span id="right">></span></div>
</div>
</div>
<script type="text/javascript">
var box = getId("box");
var screen = box.children[0];
var ul = screen.children[0];
var ol = screen.children[1];
var ulLis = ul.children;
var imgWidth = screen.offsetWidth;
var arr = getId("arr");
var arrLeft = getId("left");
var arrRight = getId("right");
var timer = null;
// 动态生成结构,根据图片的数量生成按钮
for (var i = 0; i < ulLis.length; i++) {
var li = document.createElement("li");
li.innerHTML = i + 1;
ol.appendChild(li);
}
var olLis = ol.children;
olLis[0].className = "current";
var firstImg = ulLis[0].cloneNode(true);
ul.appendChild(firstImg);
// 排他
for (var i = 0; i < olLis.length; i++) {
olLis[i].index = i;
olLis[i].onmouseover = function() {
for (var j = 0; j < olLis.length; j++) {
olLis[j].className = "";
}
this.className = "current";
// 根据索引号 通过动画函数移动ul
var target = -this.index * imgWidth;
animate(ul, target);
pic = this.index;
square = this.index;
};
}
// 鼠标经过盒子显示箭头,离开隐藏
box.onmouseover = function() {
arr.style.display = "block";
clearInterval(timer);
};
box.onmouseout = function() {
arr.style.display = "none";
timer = setInterval(arrRight.onclick, 2000);
};
var pic = 0;
var square = 0;
arrRight.onclick = function() {
if (pic === ulLis.length - 1) {
ul.style.left = 0 + "px";
pic = 0;
}
pic++;
var target = -pic * imgWidth;
animate(ul, target);
if (square < olLis.length - 1) {
square++;
} else {
square = 0;
}
for (var i = 0; i < olLis.length; i++) {
olLis[i].className = "";
}
olLis[square].className = "current";
};
arrLeft.onclick = function() {
if (pic === 0) {
ul.style.left = -(ulLis.length - 1) * imgWidth + "px";
pic = ulLis.length - 1;
}
pic--;
var target = -pic * imgWidth;
animate(ul, target);
if (square > 0) {
square--;
} else {
square = olLis.length - 1;
}
for (var i = 0; i < olLis.length; i++) {
olLis[i].className = "";
}
olLis[square].className = "current";
};
timer = setInterval(arrRight.onclick, 2000);
</script>
</body>
</html>
2.需要添加一个JS。
3.这个最重要的一段代码如下:
function animate(obj, target) {
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var leader = obj.offsetLeft;
var step = 30;
step = leader < target ? step : -step;
if (Math.abs(leader - target) > Math.abs(step)) {
leader = leader + step;
obj.style.left = leader + "px";
} else {
obj.style.left = target + "px";
clearInterval(obj.timer);
}
}, 15);
}