求助~报错了,但是没找到什么原因
来源:3-7 点击圆点切换图片(2)
choyuky
2018-01-18 16:58:09
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript实现轮播特效</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="main" id="main">
<!-- 图片轮播 -->
<div class="banner" id="banner">
<a href="">
<div class="banner-slide slide1 slide-active"></div>
</a>
<a href="">
<div class="banner-slide slide2"></div>
</a>
<a href="">
<div class="banner-slide slide3"></div>
</a>
</div>
<!-- 上一张、下一张按钮 -->
<a href="JavaScript:void(0)" class="button prev" id="prev"></a>
<a href="JavaScript:void(0)" class="button next" id="next"></a>
<!-- 圆点导航 -->
<div class="dots" id="dots">
<span class="active"></span>
<span></span>
<span></span>
</div>
</div>
<script src="js/js.js"></script>
</body>
</html>
//css文件代码
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
}
body{
font-family: "微软雅黑";
color: #14191e;
}
.main{
width: 1200px;
height: 460px;
margin: 30px auto;
overflow: hidden;
position: relative;
}
.banner{
width: 1200px;
height: 460px;
overflow: hidden;
position: relative;
}
.banner-slide{
width: 1200px;
height: 460px;
position: absolute;
background-repeat: no-repeat;
display: none;
}
.slide1{
background-image: url(../img/bg1.jpg);
}
.slide2{
background-image: url(../img/bg2.jpg);
}
.slide3{
background-image: url(../img/bg3.jpg);
}
.slide-active{
display: block;
}
.button{
position: absolute;
width: 40px;
height: 80px;
left: 244px;
top: 50%;
margin-top: -40px;
background: url(../img/arrow.png) no-repeat center center;
}
.button:hover{
background-color: #333;
opacity: 0.2;
filter: alpha(opacity=20);
}
.prev{
transform: rotate(180deg);
}
.next{
left: auto;
right: 0;
}
.dots{
position: absolute;
right: 24px;
bottom: 24px;
text-align: right;
}
.dots span{
display: inline-block;
width: 12px;
height: 12px;
line-height: 12px;
border-radius: 50%;
background: rgba(7,17,27,0.4);
box-shadow: 0 0 0 2px rgba(255,255,255,0.4) inset;/*inset内置描边*/
margin-left: 8px;
cursor: pointer;
}
.dots span.active{
background: #fff;
box-shadow: 0 0 0 2px rgba(27,17,27,0.4) inset;/*inset内置描边*/
}
//js代码
//封装一个代替getElementById()的方法
function byId(id) {
return typeof(id) === "string"?document.getElementById('id'):id;
}
//全局变量
var index = 0,
timer = null,
pics = byId("banner").getElementsByTagName('div'),
dots = byId("dots").getElementsByTagName('span'),
prev = byId("prev").getElementById('prev'),
next = byId("next").getElementById('next'),
len = pics.length;
function slideImg() {
var main = byId("main");
//滑过清除定时器,离开继续
main.onmouseover = function () {
//滑过清除定时器
if (timer) clearInterval(timer);
}
main.onmouseout = function () {
timer =setIntreval(function(){
index++; //len = 3
if (index>=len) {
index = 0;
}
//切换图片
changeImg();
},3000);
}
//自动在main上触发鼠标离开的事件
main.onmouseout();
//遍历所有点击,且绑定点击事件,点击圆点切换图片
for (var d = 0 ;d < len; d++) {
//给所有span添加一个id的属性,值为d,作为当前span的索引
dots[d].id =d;
dots[d].onclick = function(){
//改变index为当前span的id值
index = this.id;
//条用changeImg,实现切换图片
changeImg();
}
}
//下一张
next.onclick = function () {
index++;
if (index>= len) index=0;
changeImg();
}
//上一张
next.onclick = function () {
index--;
if (index<0) index=len-1;
changeImg();
}
}
//切换图片
function changeImg() {
//需要遍历banner下多有的div及dots下所有的span,将div隐藏,将span清除类
for (var i = 0; i <len; i++) {
pics[i].style.display = 'none';
dots[i].className = "";
}
//根据index索引找到当前div和当前span,将其显示出来和设为当前
pics[index].style.display = 'block';
dots[index].className = "active";
}
slideImg();
2回答
如果你是照着老师的写的话,那代码还是有所不同的
还是要理解了代码再去写。
小丸子爱吃菜
2018-01-18
语法是:document.getElementsByTagName(tagname)
前面换成document
祝学习愉快!
相似问题