图片的放大模态框

来源:互联网 发布:小米网络放大器怎么用 编辑:程序博客网 时间:2024/05/21 23:37
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        #myImg {          
            margin:30px 30%;
            border-radius: 10px;
            cursor: pointer;
            transition: 0.3s;
        }
        #myImg:hover{opacity:0.7;}
        .modal {
            display: none;
            width: 100%;
            height: 100%;
            padding-top: 100px;
            left: 0;
            top: 0;
            position:fixed;
            overflow: auto;
            z-index: 1;
            background-color: rgba(0,0,0,0.8);
        }
        .modal-content {
            max-width: 700px;
            width: 80%;
            margin: auto;
            display: block;
        }
        #caption {
            max-width: 700px;
            width: 80%;
            margin: auto;
            height: 150px;
            padding-top: 20px;
            text-align: center;
            display: block;
            color: #ff0000;
        }
        @keyframes ni{
            0%{transform:scale(0.1)}
            100%{transform:scale(1)}
        }
        .modal-content, #caption {
            -webkit-animation-name: zoom;
    -webkit-animation-duration: 2s;
            animation-name: ni;
            animation-duration: 2s;
        }
        .close {
            position: absolute;
            top: 20px;
            right: 20px;
            font-size: 40px;
            color: #ff0000;
            cursor: pointer;
        }
    </style>
<meta charset="utf-8" />
</head>
<body>
    <img src="tu1.jpg" width="400" height="400" alt="人生的意义不在于我不说" id="myImg" />
    <div id="myModal" class="modal">
        <span class="close">×</span>
        <img class="modal-content" id="img01">
        <div id="caption"></div>
    </div>
    <script>
        var modal = document.getElementById('myModal');
        // 获取图片模态框,alt 属性作为图片弹出中文本描述
        var img = document.getElementById('myImg');
        var modalImg = document.getElementById("img01");
        var captionText = document.getElementById("caption");
        img.onclick = function () {
            modal.style.display = "block";
            modalImg.src = this.src;
            modalImg.alt = this.alt;
            captionText.innerHTML = this.alt;
        }
        var span = document.getElementsByClassName("close")[0];


        // 点击 <span> 元素上的 (x), 关闭模态框
        span.onclick = function () {
            modal.style.display = "none";
        }


    </script>
</body>
</html>
原创粉丝点击