一个简单的HTML5和JavaScript代码来实现点击图片放大效果。以下是示例代码:
<!DOCTYPE html>
<html>
<head>
<title>点击图片放大效果</title>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
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>
<h2>点击图片放大效果</h2>
<img id="myImg" src="your_image.jpg" alt="Your Image" style="width:100%;max-width:300px">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
<script>
var modal = document.getElementById("myModal");
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
};
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
};
</script>
</body>
</html>
请将your_image.jpg
替换为您要展示的图片的路径。这段代码创建了一个模态框(Modal),当用户点击图片时,模态框会出现并将图片放大显示。用户可以点击模态框右上角的关闭按钮来关闭模态框。