html 美化input file,Input[type=”file”] 显示效果美化方法 | 智慧宫
Html中input可以作為上傳圖片代碼如下
但是演示非常不好看,下面做一番美化處理,效果如下:
文件上傳美化前后對(duì)比
美化方法隱藏 input 標(biāo)簽
使用 label 標(biāo)簽在input標(biāo)簽位置顯示;
用js獲取 input 中上傳的文件名稱(chēng),并在 lable 標(biāo)簽中顯示
input[type=”file”] 美化之 Html/**
* input 文件上傳 Html
*
* @author 智慧宮
* @link https://www.hanost.com
*/
input[type=”file”] 美化之 CSS
使用opacity: 0; 隱藏input標(biāo)簽,美化lable標(biāo)簽;/**
* input 文件上傳 CSS
*
* @author 智慧宮
* @link https://www.hanost.com
*/
.img-upload {
position: relative;
display: inline-block;
width: 100%;
margin-bottom: 0;
height: calc(3.5rem + 28px);
}
.img-upload-input {
position: relative;
z-index: 2;
width: 100%;
height: calc(3rem + 28px);
margin: 0;
opacity: 0;
}
.img-upload-label {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
background: #f0f0f0;
text-align: center;
padding: 1.5rem;
}
input[type=”file”] 美化之 JS
使用JS 來(lái)獲取上傳文件的名稱(chēng),并在 label 標(biāo)簽中顯示上傳的文件名/**
* input 文件上傳 JS
*
* @author 智慧宮
* @link https://www.hanost.com
*/
$(".img-upload").on("change", "#img-upload-input", function () {
var filePath = $(this).val();
if (filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
var arr = filePath.split('\\');
var fileName = arr[arr.length - 1];
$("#img-upload-label").html(fileName);
}
})
美化效果展示
input 美化效果展示
總結(jié)
以上是生活随笔為你收集整理的html 美化input file,Input[type=”file”] 显示效果美化方法 | 智慧宫的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: udp 使用connect优点_一文搞懂
- 下一篇: 图片还原去遮挡_斗罗:这届coser太强