Jump to content
  • 0

Стилизация input type и его превью


DivMan
 Share

Question

Мне надо было стилизовать input type, и что бы после загрузки, показалась превьюшка, я искал готовые решения, но они были какие-то очень большие, некоторые даже подключают большой css файл ratchet.css.

И я решил написать свой решение, хороший ли это способ?

https://jsfiddle.net/w30shtu9/

 

Скрытый текст

<div class="wrap">

	<div>
		<img src="" class="img-circle" id="theImage">
	</div>

	<input type="file" id="photoInput">

	<button class="addImg">Доб. Изображение</button>

</div>

 

Скрытый текст

.wrap {border: 1px solid; width: 500px;}

	#photoInput {
		display: none;
	}
	
	.img-circle {
		width: 400px;
		height: 210px;
		border: 1px solid;
		object-fit: cover;
	}
	
	.addImg {
		margin-top: 20px;
		border: 0;
		background: url("http://i94.fastpic.ru/big/2017/0808/6b/5c6e9d842b4ecb782a9c8132a068966b.png") no-repeat 8px 8px #0c6eb9;
		color: white;
		text-transform: uppercase;
		padding: 8px;
		padding-left: 30px;
		border-radius: 5px;
	}

 

 

Скрытый текст

var btn = document.querySelector(".addImg");
var photoFile = document.getElementById("photoInput");

btn.addEventListener('click', function() {
	photoFile.click();
})

	var fileReader = new FileReader();
		fileReader.addEventListener('load', function() {
			theImage.src = this.result;
		});

		photoInput.addEventListener('change', function(e) {
		var file = e.target.files[0];

		fileReader.readAsDataURL(file);
});

 

 

Edited by DivMan
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy