代码
- <script>
- function showImagePopup(imgUrl="1.jpg") {
- // 创建遮罩层
- const overlay = document.createElement('div');
- overlay.style = `
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- background: rgba(0,0,0,0.8);
- z-index: 999;
- display: flex;
- justify-content: center;
- align-items: center;
- `;
-
- // 创建图片容器
- const imgContainer = document.createElement('div');
- imgContainer.style = `
- max-width: 90%;
- max-height: 90%;
- position: relative;
- `;
-
- // 创建关闭按钮
- const closeBtn = document.createElement('span');
- closeBtn.innerHTML = '×';
- closeBtn.style = `
- position: absolute;
- top: -30px;
- right: -30px;
- color: white;
- font-size: 30px;
- cursor: pointer;
- `;
- closeBtn.onclick = () => document.body.removeChild(overlay);
-
- // 创建图片元素
- const img = document.createElement('img');
- img.src = imgUrl;
- img.style = `
- max-width: 100%;
- max-height: 90vh;
- border: 3px solid white;
- box-shadow: 0 0 20px rgba(0,0,0,0.5);
- `;
-
- // 组装元素
- imgContainer.appendChild(closeBtn);
- imgContainer.appendChild(img);
- overlay.appendChild(imgContainer);
- document.body.appendChild(overlay);
-
- // 点击遮罩层关闭
- overlay.onclick = (e) => {
- if (e.target === overlay) document.body.removeChild(overlay);
- };
- }
- </script>
复制代码
- onclick="showImagePopup()"
复制代码 |