Skip to content

Commit

Permalink
Фото на память (тестовая)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenapokrovskaya committed Jan 20, 2025
1 parent d8c90d7 commit a903481
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2 class="img-upload__title visually-hidden">Загрузка фотограф

<!-- Изначальное состояние поля для загрузки изображения -->
<fieldset class="img-upload__start">
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename" required>
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename" accept="image/png, image/jpeg" required>
<label for="upload-file" class="img-upload__label img-upload__control">Загрузить</label>
</fieldset>

Expand Down
18 changes: 18 additions & 0 deletions js/avatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const FILE_TYPES = ['gif', 'jpg', 'jpeg', 'png'];

const avatarElement = document.querySelector('.img-upload__preview img');
const avatarPreviews = document.querySelectorAll('.effects__preview');
const uploadFileInputElement = document.querySelector('#upload-file');

uploadFileInputElement.addEventListener('change', () => {
const file = uploadFileInputElement.files[0];
//получаем имя файла через name и приводим к одному виду
const fileName = file.name.toLowerCase();
//Делаем проверку совпадает ли имя нашего файла с допустимыми
const matches = FILE_TYPES.some((item) => fileName.endsWith(item));

if(matches) {
avatarElement.src = URL.createObjectURL(file);
avatarPreviews.forEach((el) => el.style.setProperty('background-image', `url(${URL.createObjectURL(file)})`));
}
});
1 change: 1 addition & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {createFragment} from './thumbnails.js';
import {renderModal} from './modal.js';
import {showDataError} from './notifications.js';
import {onChangeFilterPosts} from './filter.js';
import './avatar.js';

const ERROR_DISPLAY_TIME = 5000;
const templateDataErrorElement = document.querySelector('#data-error').content.querySelector('.data-error');
Expand Down

0 comments on commit a903481

Please sign in to comment.