作爲我工作中的一個項目的一部分,我試圖做一個非常簡單的圖片滑塊。我的href onclick事件不起作用
我做了一個功能,應該做幻燈片,但問題是當我點擊箭頭時,它不會將圖像更改爲下一個/上一個。
這裏是我的代碼:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Simple Photo Slider</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>
<body>
<div class="container">
<!--Left Arrow-->
<div class="arrow">
<a href="#" onclick="slide(-1)">
<span class="left"></span>
</a>
</div>
<!--Image-->
<img src="images/img1.jpg" class="img" />
<!--Right Arrow-->
<div class="arrow">
<a href="#" onclick="slide(1)">
<span class="right"></span>
</a>
</div>
</div>
<script src="js/index.js"></script>
</body>
</html>
JS:
var imageCount = 1;
var total = 5;
function slide(x)
{
var image = document.getElementsByClassName('img');
imageCount += x;
if (imageCount > total || imageCount < 1)
imageCount = 1;
image.src = "images/img" + imageCount + ".jpg";
}
CSS:
.container {
width:700px;
height:600px;
margin: 20px auto;
position:relative;
}
.arrow {
display: inline-block;
vertical-align: middle;
position:absolute;
top:40%;
width:3em;
}
a {
display: inline-block;
border-radius: 50%;
text-align:center;
}
a:hover .left, a:hover .top, a:hover .bottom, a:hover .right{
border: 0.3em solid #e74c3c;
}
a:hover .left:after, a:hover .top:after, a:hover .bottom:after, a:hover .right:after {
border-top: 0.3em solid #e74c3c;
border-right: 0.3em solid #e74c3c;
}
.left {
display: inline-block;
width: 3em;
height: 3em;
border: 0.3em solid #333;
border-radius: 50%;
/* margin-right: -3.5em; */
/* margin-left: 0em; */
/* margin: auto 0; */
}
.left:after {
content: '';
display: inline-block;
margin-top: 0.7em;
margin-left: 0.6em;
width: 1.4em;
height: 1.4em;
border-top: 0.3em solid #333;
border-right: 0.3em solid #333;
-moz-transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
transform: rotate(-135deg);
}
.right {
display: inline-block;
width: 3em;
height: 3em;
border: 0.3em solid #333;
border-radius: 50%;
margin-left: 40.1em;
}
.right:after {
content: '';
display: inline-block;
margin-top: 0.7em;
margin-left: -0.6em;
width: 1.4em;
height: 1.4em;
border-top: 0.3em solid #333;
border-right: 0.3em solid #333;
-moz-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.img {
height:500px;
width:500px;
display:inline-block;
position:absolute;
left:14%;
}
我怎樣才能使它發揮作用?我做錯了什麼?
'document.getElementsByClassName()'返回元素集合* *。因此,'image.src'將失敗,因爲元素集合不公開'src'屬性。 –