文件选择域

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

效果如下

Amaze UI 文件选择域的示例代码

但是官方给的方案一个问题在于,上传文件之后图标没有改变,也没有显示上传文件名的地方。

于是我做了一个小小的修改:加入一段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 文件选择域的示例代码

进一步的,如果传的是图片,我想预览上传的图片效果图呢。

那就再加一段小代码:

$('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 文件选择域的示例代码

当鼠标放上面就会自动显示上传图片的缩略图了。

转载于:https://my.oschina.net/u/3463305/blog/1504565

总结

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。