JavaScript的一个图片切换效果,点击上面的小图片会得到大的图片,只需更改照片地址即可运行出此效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#imgbox a {
width: 400px;
}
#imgbox a img {
width: 110px;
border: 0;
}
</style>
</head>
<body>
<h2>图片相册</h2>
<div id="imgbox">
<a href="./images/1.jpg" title="图片1">
<img src="./images/1.jpg" alt="易烊千玺1">
</a>
<a href="./images/2.jpg" title="图片2">
<img src="./images/2.jpg" alt="易烊千玺2">
</a>
<a href="./images/3.jpg" title="图片3">
<img src="./images/3.jpg" alt="易烊千玺3">
</a>
<a href="./images/4.jpg" title="图片4">
<img src="./images/4.jpg" alt="易烊千玺4">
</a>
</div>
<div style="clear:both"></div>
<img id="image" src="images/placeholder.png" alt="" width="450px" />
<p id="des">选择一个图片</p>
<script>
var imgbox = document.getElementById('imgbox');
var links = imgbox.getElementsByTagName('a');
var i = 0;len = links.length;
for(;i < len;i ++) {
// 获取当前元素
var link = links[i];
link.onclick = function () {
// 切换图片和文字
var image = document.getElementById('image');
var des = document.getElementById('des');
// 设置图片
image.src = this.href;
// 设置文字
des.innerHTML = this.title;
// 取消默认行为的执行
return false;
}
}
</script>
</body>
</html>