点击图片弹出放大是一种常见的网页交互效果,通常通过JavaScript和CSS实现。当用户点击图片时,会弹出一个覆盖整个页面的模态框(modal),并在其中显示放大的图片。
以下是一个简单的JavaScript和CSS实现点击图片弹出放大的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Zoom</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<img src="thumbnail.jpg" alt="Sample Image" id="thumbnail">
<div id="modal" class="modal">
<span class="close-button">×</span>
<img src="" alt="Enlarged Image" id="enlargedImage">
</div>
<script src="script.js"></script>
</body>
</html>
.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 img {
display: block;
margin: auto;
width: 80%;
max-width: 700px;
}
.close-button {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
cursor: pointer;
}
document.getElementById('thumbnail').addEventListener('click', function() {
document.getElementById('modal').style.display = 'block';
document.getElementById('enlargedImage').src = this.src;
});
document.querySelector('.close-button').addEventListener('click', function() {
document.getElementById('modal').style.display = 'none';
});
通过以上方法,可以有效实现点击图片弹出放大的功能,并解决常见的实现问题。
没有搜到相关的文章