本篇文章给大家分享的是有关使用Amaze UI怎么实现一个文件选择域,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
文件选择域
<input type="file">
也是 CSS 啃不动的一块骨头,如果实在看不惯原生的样式,一般的做法是把文件选择域设为透明那个,覆盖在其他元素。
<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>
效果如下
但是官方给的方案一个问题在于,上传文件之后图标没有改变,也没有显示上传文件名的地方。
于是我做了一个小小的修改:加入一段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]) });
}
});
上传文件后
进一步的,如果传的是图片,我想预览上传的图片效果图呢。
那就再加一段小代码:
$('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怎么实现一个文件选择域,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.jb51.net/html5/742436.html