Примеры изменения вида стандартного поля для загрузки файлов (input[type=file]
) с помощью CSS и JS.
.input-file {
position: relative;
display: inline-block;
}
.input-file-btn {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
text-decoration: none;
font-size: 14px;
vertical-align: middle;
color: rgb(255 255 255);
text-align: center;
border-radius: 4px;
background-color: #419152;
line-height: 22px;
height: 40px;
padding: 10px 20px;
box-sizing: border-box;
border: none;
margin: 0;
transition: background-color 0.2s;
}
.input-file-text {
padding: 0 10px;
line-height: 40px;
display: inline-block;
}
.input-file input[type=file] {
position: absolute;
z-index: -1;
opacity: 0;
display: block;
width: 0;
height: 0;
}
/* Focus */
.input-file input[type=file]:focus + .input-file-btn {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
/* Hover/active */
.input-file:hover .input-file-btn {
background-color: #59be6e;
}
.input-file:active .input-file-btn {
background-color: #2E703A;
}
/* Disabled */
.input-file input[type=file]:disabled + .input-file-btn {
background-color: #eee;
}
Результат:
.input-file {
position: relative;
display: inline-block;
}
.input-file span {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
text-decoration: none;
font-size: 14px;
vertical-align: middle;
color: rgb(255 255 255);
text-align: center;
border-radius: 4px;
background-color: #419152;
line-height: 22px;
height: 40px;
padding: 10px 20px;
box-sizing: border-box;
border: none;
margin: 0;
transition: background-color 0.2s;
}
.input-file input[type=file] {
position: absolute;
z-index: -1;
opacity: 0;
display: block;
width: 0;
height: 0;
}
/* Focus */
.input-file input[type=file]:focus + span {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
/* Hover/active */
.input-file:hover span {
background-color: #59be6e;
}
.input-file:active span {
background-color: #2E703A;
}
/* Disabled */
.input-file input[type=file]:disabled + span {
background-color: #eee;
}
Результат:
.input-file {
position: relative;
display: inline-block;
}
.input-file-text {
padding: 0 10px;
line-height: 40px;
text-align: left;
height: 40px;
display: block;
float: left;
box-sizing: border-box;
width: 200px;
border-radius: 6px 0px 0 6px;
border: 1px solid #ddd;
}
.input-file-btn {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
text-decoration: none;
font-size: 14px;
vertical-align: middle;
color: rgb(255 255 255);
text-align: center;
border-radius: 0 4px 4px 0;
background-color: #419152;
line-height: 22px;
height: 40px;
padding: 10px 20px;
box-sizing: border-box;
border: none;
margin: 0;
transition: background-color 0.2s;
}
.input-file input[type=file] {
position: absolute;
z-index: -1;
opacity: 0;
display: block;
width: 0;
height: 0;
}
/* Focus */
.input-file input[type=file]:focus + .input-file-btn {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
/* Hover/active */
.input-file:hover .input-file-btn {
background-color: #59be6e;
}
.input-file:active .input-file-btn {
background-color: #2E703A;
}
/* Disabled */
.input-file input[type=file]:disabled + .input-file-btn {
background-color: #eee;
}
Результат:
.input-file-row {
display: inline-block;
}
.input-file {
position: relative;
display: inline-block;
}
.input-file span {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
text-decoration: none;
font-size: 14px;
vertical-align: middle;
color: rgb(255 255 255);
text-align: center;
border-radius: 4px;
background-color: #419152;
line-height: 22px;
height: 40px;
padding: 10px 20px;
box-sizing: border-box;
border: none;
margin: 0;
transition: background-color 0.2s;
}
.input-file input[type=file] {
position: absolute;
z-index: -1;
opacity: 0;
display: block;
width: 0;
height: 0;
}
/* Focus */
.input-file input[type=file]:focus + span {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
/* Hover/Active */
.input-file:hover span {
background-color: #59be6e;
}
.input-file:active span {
background-color: #2E703A;
}
/* Disabled */
.input-file input[type=file]:disabled + span {
background-color: #eee;
}
/* Список файлов */
.input-file-list {
padding: 10px 0;
}
.input-file-list-item {
margin-bottom: 10px;
}
.input-file-list-remove {
color: red;
text-decoration: none;
display: inline-block;
margin-left: 5px;
}
var dt = new DataTransfer();
$('.input-file input[type=file]').on('change', function(){
let $files_list = $(this).closest('.input-file').next();
$files_list.empty();
for(var i = 0; i < this.files.length; i++){
let new_file_input = '<div class="input-file-list-item">' +
'<span class="input-file-list-name">' + this.files.item(i).name + '</span>' +
'<a href="#" onclick="removeFilesItem(this); return false;" class="input-file-list-remove">x</a>' +
'</div>';
$files_list.append(new_file_input);
dt.items.add(this.files.item(i));
};
this.files = dt.files;
});
function removeFilesItem(target){
let name = $(target).prev().text();
let input = $(target).closest('.input-file-row').find('input[type=file]');
$(target).closest('.input-file-list-item').remove();
for(let i = 0; i < dt.items.length; i++){
if(name === dt.items[i].getAsFile().name){
dt.items.remove(i);
}
}
input[0].files = dt.files;
}
.input-file-row {
display: inline-block;
}
.input-file {
position: relative;
display: inline-block;
}
.input-file span {
position: relative;
display: inline-block;
cursor: pointer;
outline: none;
text-decoration: none;
font-size: 14px;
vertical-align: middle;
color: rgb(255 255 255);
text-align: center;
border-radius: 4px;
background-color: #419152;
line-height: 22px;
height: 40px;
padding: 10px 20px;
box-sizing: border-box;
border: none;
margin: 0;
transition: background-color 0.2s;
}
.input-file input[type=file] {
position: absolute;
z-index: -1;
opacity: 0;
display: block;
width: 0;
height: 0;
}
/* Focus */
.input-file input[type=file]:focus + span {
box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
}
/* Hover/active */
.input-file:hover span {
background-color: #59be6e;
}
.input-file:active span {
background-color: #2E703A;
}
/* Disabled */
.input-file input[type=file]:disabled + span {
background-color: #eee;
}
/* Список c превью */
.input-file-list {
padding: 10px 0;
}
.input-file-list-item {
display: inline-block;
margin: 0 15px 15px;
width: 150px;
vertical-align: top;
position: relative;
}
.input-file-list-item img {
width: 150px;
}
.input-file-list-name {
text-align: center;
display: block;
font-size: 12px;
text-overflow: ellipsis;
overflow: hidden;
}
.input-file-list-remove {
color: #fff;
text-decoration: none;
display: inline-block;
position: absolute;
padding: 0;
margin: 0;
top: 5px;
right: 5px;
background: #ff0202;
width: 16px;
height: 16px;
text-align: center;
line-height: 16px;
border-radius: 50%;
}
var dt = new DataTransfer();
$('.input-file input[type=file]').on('change', function(){
let $files_list = $(this).closest('.input-file').next();
$files_list.empty();
for(var i = 0; i < this.files.length; i++){
let file = this.files.item(i);
dt.items.add(file);
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function(){
let new_file_input = '<div class="input-file-list-item">' +
'<img class="input-file-list-img" src="' + reader.result + '">' +
'<span class="input-file-list-name">' + file.name + '</span>' +
'<a href="#" onclick="removeFilesItem(this); return false;" class="input-file-list-remove">x</a>' +
'</div>';
$files_list.append(new_file_input);
}
};
this.files = dt.files;
});
function removeFilesItem(target){
let name = $(target).prev().text();
let input = $(target).closest('.input-file-row').find('input[type=file]');
$(target).closest('.input-file-list-item').remove();
for(let i = 0; i < dt.items.length; i++){
if(name === dt.items[i].getAsFile().name){
dt.items.remove(i);
}
}
input[0].files = dt.files;
}
Но здесь да, эта строчка совсем лишняя!
Я бы ещё в начало JS добавил строку, которая удалит при перезагрузке страницы прикреплённые файлы
сервер, то получим пустой результат.
Добавим строку загрузки на сервер с type="submit" и в проге load_file.php получим пустые данные
<form action="load_file.php" enctype="multipart/form-data" method="post">
<input type="file" name="path" multiple accept="image/*">
<input type="submit" value="upload" name="path" >
А ведь наша главная цель загрузить изображение на сервер !?