在JavaScript中设置点击图片查看大图的功能,通常涉及以下几个基础概念和技术点:
以下是一个简单的示例,展示了如何实现点击图片查看大图的功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Viewer</title>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.9);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<img id="myImg" src="small.jpg" alt="Image description" width="300" height="200">
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
<script>
// JavaScript部分
document.getElementById('myImg').onclick = function(){
document.getElementById('myModal').style.display = "block";
document.getElementById('img01').src = this.src;
}
document.getElementsByClassName('close')[0].onclick = function(){
document.getElementById('myModal').style.display = "none";
}
</script>
</body>
</html>
通过以上步骤和示例代码,你可以轻松实现一个基本的点击图片查看大图的功能,并根据实际需求进行扩展和优化。
领取专属 10元无门槛券
手把手带您无忧上云