前端实现点击图片放大查看,并点击关闭

效果展示

在这里插入图片描述

HTML 代码

HTML代码比较简单,包含了一个img元素,用显示原有图片,和一个模态框元素div,用于显示放大之后的图片元素,模拟模态框的样式

<!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="index.css">
</head>

<body>
    <img id="image" src="https://tse3-mm.cn.bing.net/th/id/OIP-C.9Pt31ku3_vc9N5TlSzIQYAHaEK?w=284&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt="Image" onclick="zoomImage()">
    <div id="modal" class="modal" onclick="closeModal()">
        <span class="close" onclick="closeModal()">&times;</span>
        <img class="modal-content" id="modalImage">
    </div>
    <script src="index.js"></script>
</body>

</html>

CSS

CSS样式比较常规,正常模态框的样式:背景颜色稍暗,铺满整页,凸显放大后的图片。这里为模态框添加了动画,当点击图片时,模态框出现不会显得那么生硬

#image {
    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.1);
}

.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;
    cursor: pointer;
}

/*兼容性写法, 确保在webkit内核的浏览器能正常使用*/
@-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: burlywood;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: white;
    text-decoration: none;
    cursor: pointer;
}

JS

JS代码就有两个功能,点击后,然模态框可见,以及模态框的关闭

//显示模态框
function zoomImage() {
    var modal = document.getElementById("modal");
    var modalImg = document.getElementById("modalImage");
    var img = document.getElementById("image");
    modal.style.display = "block";
    modalImg.src = img.src;
}
//关闭模态框
function closeModal() {
    var modal = document.getElementById("modal");
    modal.style.display = "none";
}

相关推荐

  1. Uniapp 图片放大

    2024-06-10 10:00:03       16 阅读
  2. Android Glide从网络加载图片 放大查看效果

    2024-06-10 10:00:03       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-10 10:00:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-10 10:00:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-10 10:00:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-10 10:00:03       20 阅读

热门阅读

  1. 006 CentOS 7.9 elasticsearch7.10.0安装及配置

    2024-06-10 10:00:03       11 阅读
  2. 1341. 电影评分

    2024-06-10 10:00:03       12 阅读
  3. 如何学好量子计算机技术的两种思路

    2024-06-10 10:00:03       7 阅读
  4. 爬山算法详细介绍

    2024-06-10 10:00:03       12 阅读
  5. 4. 流程控制语句

    2024-06-10 10:00:03       10 阅读
  6. vscode 好用的插件

    2024-06-10 10:00:03       12 阅读