App下載

在html5的AmazeUI中怎么實現(xiàn)文件選擇域效果?實現(xiàn)案例詳解!

猿友 2021-08-05 11:30:49 瀏覽數(shù) (1924)
反饋

我們在日常各種網(wǎng)頁中上傳文件都是通過選擇進行上傳的,那么今天我們來講講有關(guān)于:“ 在html5的AmazeUI中怎么實現(xiàn)文件選擇域效果? ”這個問題吧!下面是有關(guān)于這個問題的內(nèi)容分享!

文件選擇域

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

<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>

效果如下:

145948_OwIf_3463305.png

但是官方給的方案一個問題在于,上傳文件之后圖標沒有改變,也沒有顯示上傳文件名的地方。

于是我做了一個小小的修改:加入一段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]) });
    }
});

上傳文件后

150410_3db7_3463305.png

進一步的,如果傳的是圖片,我想預(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();
});

151047_NSDO_3463305.png

當鼠標放上面就會自動顯示上傳圖片的縮略圖了。

轉(zhuǎn)載于:https://my.oschina.net/u/3463305/blog/1504565

總結(jié)

那么上面就是有關(guān)于:“在html5的AmazeUI中怎么實現(xiàn)文件選擇域效果?”這個問題的相關(guān)內(nèi)容分享!在大家閱讀問之后想必就知道了怎么實現(xiàn)文件選擇這個方法了,更多關(guān)于Amaze UI這方面的相關(guān)內(nèi)容我們都可以在W3Cschool中進行學習!

0 人點贊