Skip to content

Instantly share code, notes, and snippets.

@tronghuan98hd
Last active May 7, 2020 03:58
Show Gist options
  • Save tronghuan98hd/bc6e05de914607a707a21e73976ad143 to your computer and use it in GitHub Desktop.
Save tronghuan98hd/bc6e05de914607a707a21e73976ad143 to your computer and use it in GitHub Desktop.
view xem trước ảnh và có thể đổi tên
var selDiv = "";
document.addEventListener("DOMContentLoaded", init, false);
var selImg = -1;
var filesArr = [];
function init() {
document.querySelector('#file').addEventListener('change', handleFileSelect, false);
selDiv = document.querySelector("#selectedFiles");
}
function handleFileSelect(e) {
selDiv = document.querySelector("#selectedFiles");
if (!e.target.files || !window.FileReader) return;
selDiv.innerHTML = "";
var files = e.target.files;
filesArr = Array.prototype.slice.call(files);
filesArr.forEach((f, index) => {
if (!f.type.match("image.*")) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var html = "<div class='border-preview id=''" + index +
"'><a href = '#" + index +
"'><img class='preview' src='" + URL.createObjectURL(f) +
"' id= " + index +
"></a><span class='glyphicon glyphicon-remove-circle removeImage' id='" + index +
"'></span></i><input type='text' id='id_name' name='name' value='" + f.name + "'>" +
"<br clear=\"left\"/></div>";
selDiv.innerHTML += html;
$(".preview").click(function(e) {
e.preventDefault;
var newLinkImage = document.getElementById("large-preview");
newLinkImage.src = $(this).context.src;
if (selImg >= 0) {
document.getElementById(selImg).style.border = "5px white solid";
}
selImg = $(this).context.id;
var selectImg = document.getElementById(selImg);
selectImg.style.border = "5px blue solid";
});
$(".removeImage").click(function(e) {
e.preventDefault;
$('#' + $(this).context.id + '.border-preview').remove();
});
}
reader.readAsDataURL(f);
});
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Super Preview Form</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="scrollbar" id="style-1">
<div class="force-overflow">
<form>
<div id="get-image">
<div class="border-preview">
<img id="1" src="https://magicalmirai.com/2020/images/index_contents_mainvisual.png" class="preview">
</div>
<div class="border-preview">
<img id="2" src="https://magicalmirai.com/2020/images/index_main_bg.jpg" class="preview">
</div>
</div>
<div class="submit-form">
<input type="submit">
</div>
<script language="JavaScript">
var selImg = -1;
$(".preview").click(function(e) {
e.preventDefault;
var newLinkImage = document.getElementById("large-preview");
newLinkImage.src = $(this).context.src;
// document.getElementById(selImg).style.border = "white";
selImg = $(this).context.id;
var selectImg = document.getElementById(selImg);
selectImg.style.border = "5px blue solid";
});
</script>
</form>
</div>
</div>
</div>
<div class="col-sm-9">
<div>
<img class="large-preview" id="large-preview">
</div>
</div>
</div>
</div>
</body>
</html>
.container {
border-style: solid;
height: 600px;
}
.col-sm-3 {
border-style: solid;
border-color: red;
margin-top: 20px;
margin-bottom: 20px;
height: 550px
}
.col-sm-9 {
border-style: solid;
border-color: blue;
margin-top: 20px;
margin-bottom: 20px;
height: 550px
}
.preview {
max-width: 125px;
max-height: 125px;
float: left;
margin-bottom: 10px;
position: center;
margin-left: 10px;
margin-top: 10px;
}
.border-preview {
width: 150px;
height: 150px;
position: center;
padding: auto;
}
.large-preview {
height: 500px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
padding-top: 20px;
}
.btn-outline-primary {
margin-top: 5px;
}
.scrollbar {
background-color: #F5F5F5;
float: left;
height: 410px;
margin-bottom: 25px;
margin-left: 2px;
margin-top: 20px;
width: 100%;
overflow-y: scroll;
}
.force-overflow {
min-height: 450px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment