首页  >  文章  >  web前端  >  JavaScript 中的简单图像查看器

JavaScript 中的简单图像查看器

DDD
DDD原创
2024-10-25 03:22:02300浏览

Simple Image Viewer in JavaScript

这是一个在网络浏览器中运行的非常简单的图像查看器。它使用单个 .html 文件和 36 行代码。将代码另存为index.html - 单击此文件将在浏览器中打开一个窗口,允许您从电脑中选择要显示的图像。我已经能够打开 1024 x 1024 的图像 - 很好。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Image Viewer</title>
    <style>
        body {
            background-color: #f0f0f0;
            text-align: center;
            font-family: Arial, sans-serif;
        }
        img {
            max-width: 100%;
            height: auto;
            border: 2px solid #000;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>Simple Image Viewer</h1>
    <input type="file" id="fileInput" accept="image/*">
    <img id="imageViewer" src="#" alt="Your image will appear here.">
    <script>
        const fileInput = document.getElementById('fileInput');
        const imageViewer = document.getElementById('imageViewer');

        fileInput.addEventListener('change', function() {
            const file = this.files[0];
            const url = URL.createObjectURL(file);
            imageViewer.src = url;
        });
    </script>
</body>
</html>


本·桑托拉 - 2024 年 10 月

以上是JavaScript 中的简单图像查看器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn