溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Amaze UI怎么實(shí)現(xiàn)一個(gè)文件選擇域

發(fā)布時(shí)間:2021-04-14 16:06:26 來(lái)源:億速云 閱讀:175 作者:Leah 欄目:web開(kāi)發(fā)

本篇文章給大家分享的是有關(guān)使用Amaze UI怎么實(shí)現(xiàn)一個(gè)文件選擇域,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

文件選擇域

<input type="file"> 也是 CSS 啃不動(dòng)的一塊骨頭,如果實(shí)在看不慣原生的樣式,一般的做法是把文件選擇域設(shè)為透明那個(gè),覆蓋在其他元素。

<div class="am-form-group am-form-file">
  <button type="button" class="am-btn am-btn-default am-btn-sm">
    <i class="am-icon-cloud-upload"></i> 選擇要上傳的文件</button>
  <input type="file" multiple>
</div>
 
<hr/>
 
<div class="am-form-group am-form-file">
  <i class="am-icon-cloud-upload"></i> 選擇要上傳的文件
  <input type="file" multiple>
</div>

效果如下

使用Amaze UI怎么實(shí)現(xiàn)一個(gè)文件選擇域

但是官方給的方案一個(gè)問(wèn)題在于,上傳文件之后圖標(biāo)沒(méi)有改變,也沒(méi)有顯示上傳文件名的地方。

于是我做了一個(gè)小小的修改:加入一段js代碼

$('input[type="file"]').change(function (event) {
    var that = this;
    var file = $(that)[0].files[0];
    if(file){
        $(that).prev().text(that.files[0].name);
        $(that).attr({ 'src': window.URL.createObjectURL(that.files[0]) });
    }
});

上傳文件后

使用Amaze UI怎么實(shí)現(xiàn)一個(gè)文件選擇域

進(jìn)一步的,如果傳的是圖片,我想預(yù)覽上傳的圖片效果圖呢。

那就再加一段小代碼:

$('input[type="file"]').closest('div').hover(function(){
    if($(this).find('input[type="file"]').attr('src')){
        $('body').append('<div class="imgView" style="width: '+$(this).find('button').css('width')+';top: '+($(this).find('button').offset().top-210)+'px;left: '+$(this).find('button').offset().left+'px;height: 200px;position: absolute;text-align: center;line-height: 200px;z-index: 99999;background-color: rgba(51, 51, 51,0.6);"><img style="max-width: 90%;max-height: 90%;" src="'+$(this).find('input[type="file"]').attr('src')+'"></div>')         
    }
},function(){
    $('.imgView').remove();
});

以上就是使用Amaze UI怎么實(shí)現(xiàn)一個(gè)文件選擇域,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI