点击图片1.全屏阅览2.下载3.关闭 纯纯html css js

要实现图片点击全屏预览的功能,可以使用JavaScript和CSS来实现。以下是一个简单的示例代码:

html

go 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charsett="UTF-8">
    <title>图片点击全屏预览</title>
    <style>
        /* 全屏预览样式 */
        .fullscreen {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }

        .fullscreen img {
            max-width: 90%;
            max-height: 90%;
        }

        .fullscreen img:hover {
            cursor: pointer;
        }

        /* 关闭按钮样式 */
        .close-btn {
            position: absolute;
            top: 10px;
            right: 10px;
            color: #fff;
            font-size: 24px;
            cursor: pointer;
        }

        /* 下载按钮样式 */
        .download-btn {
            position: absolute;
            bottom: 10px;
            right: 10px;
            color: #fff;
            font-size: 24px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <img src="image.jpg" alt="图片" onclick="openFullscreen(this)">
    
    <script>
        function openFullscreen(img) {
            // 创建全屏预览容器
            var fullscreenDiv = document.createElement("div");
            fullscreenDiv.classList.add("fullscreen");

            // 创建关闭按钮
            var closeBtn = document.createElement("span");
            closeBtn.classList.add("close-btn");
            closeBtn.innerHTML = "&times;";
            closeBtn.onclick = function() {
                closeFullscreen();
            };
            fullscreenDiv.appendChild(closeBtn);

            // 创建下载按钮
            var downloadBtn = document.createElement("span");
            downloadBtn.classList.add("download-btn");
            downloadBtn.innerHTML = "&#x2193;";
            downloadBtn.onclick = function() {
                downloadImage(img.src);
            };
            fullscreenDiv.appendChild(downloadBtn);

            // 创建图片元素
            var fullscreenImg = document.createElement("img");
            fullscreenImg.src = img.src;
            fullscreenDiv.appendChild(fullscreenImg);

            // 添加全屏预览容器到页面
            document.body.appendChild(fullscreenDiv);

            // 禁用滚动
            document.body.style.overflow = "hidden";
        }

        function closeFullscreen() {
            // 移除全屏预览容器
            var fullscreenDiv = document.querySelector(".fullscreen");
            fullscreenDiv.parentNode.removeChild(fullscreenDiv);

            // 启用滚动
            document.body.style.overflow = "auto";
        }

        function downloadImage(src) {
            // 创建一个隐藏的链接并设置下载属性
            var link = document.createElement("a");
            link.href = src;
            link.download = "image.jpg";
            link.style.display = "none";

            // 将链接添加到页面并模拟点击
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
    </script>
</body>
</html>

在上面的代码中,我们首先定义了一个全屏预览的样式,并在点击图片时调用openFullscreen函数。该函数会创建一个全屏预览容器,并在容器中显示图片。同时,我们还创建了关闭按钮和下载按钮,分别用于关闭全屏预览和下载图片。

点击关闭按钮时,调用closeFullscreen函数,移除全屏预览容器,并启用滚动。

点击下载按钮时,调用downloadImage函数,创建一个隐藏的链接,并设置链接的下载属性,然后模拟点击链接实现图片下载。

请注意,这只是一个简单的示例,实际的图片全屏预览功能可能需要更多的优化和处理,例如支持多张图片预览、滑动切换等。根据具体需求,您可以根据上述示例进行扩展和修改。

相关推荐
冒险的豆子36 分钟前
Proxy & Vue 响应式(合并精简版,适合面试)
前端·javascript·vue.js
Java后端的Ai之路1 小时前
前端技术Vue-18-视图中的渲染
前端·javascript·vue.js
zttbee8 小时前
echarts图表tooltip色块失效
前端·javascript·echarts
子兮曰10 小时前
CSS 复现 23 种设计风格:从拟物到液态玻璃的成本阶梯与兜底写法【实战】
前端·css·视觉设计
虚无境10 小时前
如何用AI帮我写出实用的插件
前端·javascript·ai编程
zimoyin11 小时前
在 C# 里嵌入 JavaScript:用 Jint 做可热更新的自动化脚本(从入门到踩坑)
javascript·c#·自动化
FRing0312 小时前
JavaScript入门到精通二十六(变量提升、函数剩余参数、展开运算符、ES6箭头函数)
开发语言·前端·javascript·es6·箭头函数
OpsEye13 小时前
怎么实现大模型的按用户、按应用配额限流?
javascript·ai编程
云舜言传14 小时前
Bloom 手势演示操作 · 录屏指南
javascript·深度学习·机器学习
Highcharts.js17 小时前
BI可视化图表“维护难”问题的解决方案
开发语言·前端·javascript·编辑器·ecmascript·bi·highcharts