鼠标点击图片放大是一种常见的网页交互效果,通过JavaScript实现。当用户点击图片时,图片会放大显示,通常会覆盖整个屏幕或以某种弹窗形式展示。
以下是一个简单的JavaScript示例,使用原生JavaScript实现点击图片放大的功能:
<!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>
<style>
#image {
width: 200px;
height: auto;
cursor: pointer;
}
#zoomed-image {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
justify-content: center;
align-items: center;
}
#zoomed-image img {
max-width: 90%;
max-height: 90%;
}
</style>
</head>
<body>
<img id="image" src="path/to/your/image.jpg" alt="Sample Image">
<div id="zoomed-image">
<img id="zoomed-img" src="path/to/your/image.jpg" alt="Zoomed Image">
</div>
<script>
document.getElementById('image').addEventListener('click', function() {
document.getElementById('zoomed-image').style.display = 'flex';
});
document.getElementById('zoomed-image').addEventListener('click', function() {
this.style.display = 'none';
});
</script>
</body>
</html>通过以上方法,可以有效实现并优化鼠标点击图片放大的功能,提升用户体验。