Utwórz projekt ↗
HTML / CSS / JS / LAB · KOD NA LICENCJI MIT

Przeciąganie zdjęcia z podglądem

Lokalny podgląd pliku wybranego lub przeciągniętego do pola.

Edytuj HTML, CSS i JS ↗ Otwórz konfigurator ↗

Gdzie wykorzystać ten przykład?

Rejestracja, ankiety i konfiguratory. Wariant „Przeciąganie zdjęcia z podglądem” możesz dopasować do kolorów i treści swojej strony.

Jak działa ten efekt?

JavaScript porządkuje pola, waliduje wartości i pomaga przejść przez kolejne etapy formularza.

Jak dodać go na stronę?

  1. Otwórz przykład w edytorze i podmień tekst oraz kolory.
  2. HTML wstaw w miejscu, w którym ma pojawić się element.
  3. CSS przenieś do arkusza stylów swojej strony. Reguły body z podglądu dopasuj do własnego układu.
  4. JavaScript uruchom po załadowaniu elementów HTML, na przykład przed zamknięciem </body>.
  5. Sprawdź działanie na telefonie, klawiaturą i z własną treścią.

Na co zwrócić uwagę?

Walidację i uprawnienia sprawdzaj także na serwerze. Lokalny podgląd nie wysyła danych do backendu.

Co możesz zmienić w konfiguratorze?

Nagłówek, Kolor akcentu, Zaokrąglenie. W edytorze kodu możesz dodatkowo zmienić całą strukturę i zachowanie przykładu.

Gotowy kod: Przeciąganie zdjęcia z podglądem

HTML

<section class="lab"><h2>Przeciąganie zdjęcia z podglądem</h2><label class="drop-zone">Upuść zdjęcie lub wybierz plik<input type="file" accept="image/*"></label><div class="drop-preview"></div><p class="drop-status">Plik nie jest wysyłany na serwer.</p></section>

CSS

*{box-sizing:border-box}body{margin:0;padding:35px 24px;background:#f4f5ef;color:#293322;font:15px/1.7 Arial,sans-serif;min-height:100vh;display:grid;place-items:center}button,input{font:inherit}button,a{touch-action:manipulation}button{cursor:pointer}button:focus-visible,a:focus-visible,summary:focus-visible{outline:3px solid #738858;outline-offset:4px}h2,h3,p{margin-top:0}[hidden]{display:none!important}@media(prefers-reduced-motion:reduce){*,*:before,*:after{animation:none!important;transition:none!important;scroll-behavior:auto!important}}
.lab{max-width:800px;margin:25px auto;padding:25px;border:1px solid #d4dec9;border-radius:14px;background:#fff;color:#273724;font:15px/1.6 system-ui,sans-serif;overflow-wrap:anywhere}.lab *{box-sizing:border-box}.lab h2{font-size:23px;margin:0 0 25px;line-height:1.3}.lab h3{line-height:1.3}.lab label{display:block;margin:10px 0}.lab input:not([type=checkbox]):not([type=radio]),.lab select,.lab textarea{font:inherit;padding:9px;border:1px solid #bac9ad;border-radius:6px;max-width:100%;background:#fff;color:#273724}.lab input[type=range]{width:100%;accent-color:#6f8e58}.lab input[type=color]{height:45px;width:70px}.lab input[type=checkbox]{accent-color:#6f8e58;margin-right:8px}.lab textarea{width:100%}.lab button{font:inherit;font-weight:600;border:0;border-radius:14px;background:#6f8e58;color:#fff;padding:10px 16px;cursor:pointer}.lab button:disabled{opacity:.45;cursor:default}.lab :focus-visible{outline:3px solid #315f8b;outline-offset:3px}.lab pre{white-space:pre-wrap;overflow-wrap:anywhere;background:#f0f4e9;padding:15px;font:12px/1.6 monospace}.lab table{border-collapse:collapse;width:100%}.lab th,.lab td{padding:10px;text-align:left;border-bottom:1px solid #d9e3ce}.lab progress{accent-color:#6f8e58}.lab .lab-hint{font-size:11px;color:#687861;border-top:1px solid #dbe3d0;padding-top:16px;margin-top:25px}.lab [hidden]{display:none!important}@media(max-width:480px){.lab{padding:16px;margin:10px auto}.lab h2{font-size:20px}.lab table{font-size:12px}.lab th,.lab td{padding:7px}}@media(prefers-reduced-motion:reduce){.lab *{animation:none!important;transition:none!important}}
.drop-zone{display:block;text-align:center;padding:40px 15px;border:2px dashed #a8bc93;background:#eef4e7}.drop-zone.over{border-color:#6f8e58}.drop-zone input{display:block;max-width:100%;margin:20px auto 0}.drop-preview img{display:block;max-width:100%;max-height:260px;margin:20px auto}

JavaScript

const zone=document.querySelector('.drop-zone'),preview=document.querySelector('.drop-preview');let url;function show(file){if(!file||!file.type.startsWith('image/')){document.querySelector('.drop-status').textContent='Wybierz plik graficzny.';return}if(url)URL.revokeObjectURL(url);url=URL.createObjectURL(file);const img=document.createElement('img');img.src=url;img.alt='Podgląd wybranego zdjęcia';preview.replaceChildren(img);document.querySelector('.drop-status').textContent=file.name+' · '+Math.round(file.size/1024)+' KB · tylko lokalnie'}zone.querySelector('input').onchange=e=>show(e.target.files[0]);zone.ondragover=e=>{e.preventDefault();zone.classList.add('over')};zone.ondragleave=()=>zone.classList.remove('over');zone.ondrop=e=>{e.preventDefault();zone.classList.remove('over');show(e.dataTransfer.files[0])};

Podobne przykłady