jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。图片弹出窗口通常是指在用户点击图片时,弹出一个新的窗口或模态框(modal)来显示该图片的放大视图。
以下是一个使用 jQuery 实现图片弹出窗口的简单示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 图片弹出窗口</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 {
display: block;
margin: auto;
max-width: 80%;
max-height: 80%;
}
.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="image.jpg" alt="Image" width="300" height="200">
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
var modal = $('#myModal');
var img = $('#myImg');
var modalImg = $('#img01');
var span = $('.close');
img.click(function(){
modal.css('display', 'block');
modalImg.attr('src', this.src);
});
span.click(function(){
modal.css('display', 'none');
});
$(window).click(function(event){
if (event.target == modal[0]) {
modal.css('display', 'none');
}
});
});
</script>
</body>
</html>
span.click(function(){ modal.css('display', 'none'); });
。.modal
的 background-color
属性,确保透明度设置正确。通过以上示例代码和常见问题解决方法,你应该能够实现一个基本的 jQuery 图片弹出窗口功能。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云