ホームページ  >  記事  >  ウェブフロントエンド  >  JavaScript のシンプルな画像ビューア

JavaScript のシンプルな画像ビューア

DDD
DDDオリジナル
2024-10-25 03:22:02300ブラウズ

Simple Image Viewer in JavaScript

これは、Web ブラウザーで動作する非常にシンプルな画像ビューアーです。単一の .html ファイルと 36 行のコードを使用します。コードをindex.htmlとして保存します。このファイルをクリックすると、ブラウザにウィンドウが開き、表示する画像をPCから選択できます。 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。