カスタムアップロードボタンでユーザーアバターをアップロードする
9656 ワード
私は最近、デフォルトのアップロードボタンをカスタマイズする方法について書いたが、私はこのトピックに詳細を探索し、他のオプションを試してみたい.
ユーザーアバターをアップロードするインターフェイスを設計しました
我々は、我々のHTMLから始めます
我々は、ユーザーのアバターのいくつかのスタイルを与える.
入手可能なソースコードはcodepen.ioおよびgithub
ユーザーアバターをアップロードするインターフェイスを設計しました
ハウツー
我々は、我々のHTMLから始めます
<main>
<input type="file" name="image" id="image" accept="image/*" />
<div id="preview-wrapper">
<div id="preview"></div>
<button
id="upload-button"
aria-label="upload image"
aria-describedby="image"
>
🙂
</button>
</div>
</main>
スタイリング
我々は、ユーザーのアバターのいくつかのスタイルを与える.
#avatar {
background-color: antiquewhite;
height: 200px;
width: 200px;
border: 3px solid #0af;
border-radius: 50%;
transition: background ease-out 200ms;
}
#preview {
position: relative;
}
それから、我々の入力を隠してくださいinput[type="file"] {
display: none;
}
その後、カスタムアップロードボタンbutton {
padding: 18px;
border-radius: 50%;
border: none;
cursor: pointer;
background-color: #08f;
box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2),
0px 6px 10px 0px rgba(0, 0, 0, 0.14),
0px 1px 18px 0px rgba(0, 0, 0, 0.12);
transition: background-color ease-out 120ms;
position: absolute;
right: -5%;
bottom: 0%;
}
button:hover {
background-color: #45a;
}
我々は、我々のデザインゴールに達しました🙌, しかし、我々はまだしていません.いくつかのJavaScript
初期設定
const UPLOAD_BUTTON = document.getElementById("upload-button");
const FILE_INPUT = document.querySelector("input[type=file]");
const AVATAR = document.getElementById("avatar");
カスタムボタンをクリックすると、入力要素をプログラムでクリックする必要がありますUPLOAD_BUTTON.addEventListener("click", () => FILE_INPUT.click());
我々のイメージを与えるFILE_INPUT.addEventListener("change", event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
AVATAR.setAttribute("aria-label", file.name);
AVATAR.style.background = `url(${reader.result}) center center/cover`;
};
});
された!🥳🥳🥳
入手可能なソースコードはcodepen.ioおよびgithub
Reference
この問題について(カスタムアップロードボタンでユーザーアバターをアップロードする), 我々は、より多くの情報をここで見つけました https://dev.to/bugs_bunny/upload-user-avatar-with-a-custom-upload-button-j6lテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol