A Pen by Ishan 'Fishy' Marikar on CodePen.
Created
July 22, 2021 05:19
-
-
Save ishan-marikar/5f6741caff7814cbce0ce90a8d6dfa65 to your computer and use it in GitHub Desktop.
dyWVREx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<h1>jQuery Image Upload | |
<small>with preview</small> | |
</h1> | |
<div class="avatar-upload"> | |
<div class="avatar-edit"> | |
<input type='file' id="imageUpload" accept=".png, .jpg, .jpeg" /> | |
<label for="imageUpload"></label> | |
</div> | |
<div class="avatar-delete"> | |
<input type='file' id="imageUpload" accept=".png, .jpg, .jpeg" /> | |
<label for="imageUpload"></label> | |
</div> | |
<div class="avatar-preview"> | |
<div id="imagePreview" style="background-image: url(http://i.pravatar.cc/500?img=7);"> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readURL(input) { | |
if (input.files && input.files[0]) { | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
$("#imagePreview").css( | |
"background-image", | |
"url(" + e.target.result + ")" | |
); | |
$("#imagePreview").hide(); | |
$("#imagePreview").fadeIn(650); | |
}; | |
reader.readAsDataURL(input.files[0]); | |
} | |
} | |
$("#imageUpload").change(function () { | |
readURL(this); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background: whitesmoke; | |
font-family: "Open Sans", sans-serif; | |
} | |
.container { | |
max-width: 960px; | |
margin: 30px auto; | |
padding: 20px; | |
} | |
h1 { | |
font-size: 20px; | |
text-align: center; | |
margin: 20px 0 20px; | |
} | |
h1 small { | |
display: block; | |
font-size: 15px; | |
padding-top: 8px; | |
color: gray; | |
} | |
.avatar-upload { | |
position: relative; | |
max-width: 205px; | |
margin: 50px auto; | |
} | |
.avatar-upload .avatar-edit { | |
position: absolute; | |
right: 12px; | |
z-index: 1; | |
top: 10px; | |
} | |
.avatar-upload .avatar-edit input { | |
display: none; | |
} | |
.avatar-upload .avatar-edit input + label { | |
display: inline-block; | |
width: 34px; | |
height: 34px; | |
margin-bottom: 0; | |
border-radius: 100%; | |
background: #ffffff; | |
border: 1px solid transparent; | |
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.12); | |
cursor: pointer; | |
font-weight: normal; | |
transition: all 0.2s ease-in-out; | |
} | |
.avatar-upload .avatar-edit input + label:hover { | |
background: #f1f1f1; | |
border-color: #d6d6d6; | |
} | |
.avatar-upload .avatar-edit input + label:after { | |
content: "\f040"; | |
font-family: "FontAwesome"; | |
color: #757575; | |
position: absolute; | |
top: 10px; | |
left: 0; | |
right: 0; | |
text-align: center; | |
margin: auto; | |
} | |
.avatar-upload .avatar-preview { | |
width: 192px; | |
height: 192px; | |
position: relative; | |
border-radius: 10%; | |
border: 6px solid #f8f8f8; | |
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1); | |
} | |
.avatar-upload .avatar-preview > div { | |
width: 100%; | |
height: 100%; | |
border-radius: 10%; | |
background-size: cover; | |
background-repeat: no-repeat; | |
background-position: center; | |
} | |
.avatar-upload .avatar-delete { | |
position: absolute; | |
right: 12px; | |
z-index: 1; | |
top: 50px; | |
} | |
.avatar-upload .avatar-delete input { | |
display: none; | |
} | |
.avatar-upload .avatar-delete input + label { | |
display: inline-block; | |
width: 34px; | |
height: 34px; | |
margin-bottom: 0; | |
border-radius: 100%; | |
background: #ffffff; | |
border: 1px solid transparent; | |
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.12); | |
cursor: pointer; | |
font-weight: normal; | |
transition: all 0.2s ease-in-out; | |
} | |
.avatar-upload .avatar-delete input + label:hover { | |
background: #f1f1f1; | |
border-color: #d6d6d6; | |
} | |
.avatar-upload .avatar-delete input + label:after { | |
content: "\f040"; | |
font-family: "FontAwesome"; | |
color: #757575; | |
position: absolute; | |
top: 10px; | |
left: 0; | |
right: 0; | |
text-align: center; | |
margin: auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment