前端实现多张图片下点击图片进行放大查看

在上一篇文章中,讲到了单张图片,如何实现点击放大查看,很多读者询问了关于如何在多张图片中实现相同的效果,多张图片需要注意的是,不能给每张图片添加相同的id,因为id是唯一的,所以可以用类名代替,并为每张图片添加事件监听器。

效果展示

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7115a5c684e54c06b8e328fdb22ff4e5.gif

一、HTML 代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多张图片点击放大查看</title>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <img class="zoomable" src="https://tse2-mm.cn.bing.net/th/id/OIF-C.pCiToCj12cyN1rDRkcEfkQ?w=260&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt="Image 1">
    <img class="zoomable" src="https://tse4-mm.cn.bing.net/th/id/OIP-C.HYdftIoR4dSWe_ePzYColwHaEo?w=272&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt="Image 2">
    <img class="zoomable" src="https://tse3-mm.cn.bing.net/th/id/OIP-C.I6v11MNVmk8eBo5WFEB0WwHaEK?w=299&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt="Image 3">
    
    <!-- 模态框:放大查看后图片的位置 -->
    <div id="modal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="modalImage">
    </div>
    <script src="index.js"></script>
</body>
</html>

二、CSS 代码

css 复制代码
 .zoomable {
     width: 100%;
     max-width: 300px;
     cursor: pointer;
 }

 /* 模态框 */
 .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.2);
 }

 .modal-content {
     margin: auto;
     display: block;
     width: 80%;
     max-width: 700px;
 }

 .modal-content {
     -webkit-animation-name: zoom;
     -webkit-animation-duration: 0.6s;
     animation-name: zoom;
     animation-duration: 0.6s;
 }

 @-webkit-keyframes zoom {
     from {
         -webkit-transform: scale(0)
     }

     to {
         -webkit-transform: scale(1)
     }
 }

 @keyframes zoom {
     from {
         transform: scale(0)
     }

     to {
         transform: scale(1)
     }
 }

 /* 关闭按钮 */
 .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;
 }

三、JS 代码

js 复制代码
var modal = document.getElementById("modal");

var modalImg = document.getElementById("modalImage");

var images = document.querySelectorAll(".zoomable");

// 遍历为每个图片元素添加事件监听器
images.forEach(function (image) {
    image.addEventListener("click", function () {
        modal.style.display = "block";
        modalImg.src = this.src;
    });
});

var span = document.querySelector(".close");

span.addEventListener("click", function () {
    modal.style.display = "none";
});

modal.addEventListener("click", function (event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
});

这样就可以实现在有多张图片的情况下,也能正常实现放大查看并关闭

相关推荐
无心使然云中漫步13 小时前
Openlayers调用ArcGis地图服务之一 —— 地图切片(/tile)
前端·arcgis·vue·数据可视化
angushine13 小时前
Python常用方法
开发语言·前端·python
C澒13 小时前
AI 生码 - D2C:Figma to Code 全流程实现
前端·低代码·ai编程·figma
敲代码的鱼哇13 小时前
发送短信/拨打电话/获取联系人能力 UTS 插件(cz-sms)
android·前端·ios·uni-app·安卓·harmonyos·鸿蒙
搬搬砖得了14 小时前
Vue 响应式对象异步赋值作为 Props:二次渲染问题与组件设计哲学
前端·vue.js
张西餐14 小时前
Promise的理解
前端
天渺工作室14 小时前
别再写改名脚本了,一个 Vite 插件搞定压缩、校验、自动哈希命名vite-plugin-pack-orchestrator
前端·vite
大龄程序员狗哥14 小时前
第30篇:使用Flask部署你的第一个AI模型——打造简易Web API(项目实战)
前端·人工智能·flask
AI砖家15 小时前
解剖 Claude Code:如何搭建一个企业级的私有化 AI 编程助手
前端·人工智能·ai编程
用户57573033462415 小时前
拒绝“首屏爆炸”:用 React 哨兵模式与懒加载打造丝滑列表
前端