前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >type="file"按钮美化及获取路径 原

type="file"按钮美化及获取路径 原

作者头像
tianyawhl
发布2019-04-04 15:29:22
2.9K2
发布2019-04-04 15:29:22
举报
文章被收录于专栏:前端之攻略前端之攻略

input 里面的type="file"默认的按钮不够美观,有2种方法处理

1、自定义按钮并把<input type="file">定位在按钮的上面,并设置透明度为0,如下代码

代码语言:javascript
复制
<div class="file-input">
    <button class="btn btn-primary file-inner-btn">
        文件上传
        <i class="ion-ios-cloud-upload-outline"></i>
    </button>
    <input type="file" capture="camera" accept="image/*" name="logo" id="file">
</div>
代码语言:javascript
复制
 .file-input {
     position: relative;
     width: 100px;
 }

 .file-input button {
     width: 100%;
     height: 28px;
     line-height: 28px;
     color: #fff;
     background: #0fd5d3;
     border: none;
 }

 .file-input button:hover {
     background: #07b9b7;
 }

 .file-input button:focus {
     outline: none;
 }

 .file-input input {
     position: absolute;
     left: 0;
     top: 0;
     width: 100%;
     height: 100%;
     opacity: 0;
 }

但是这种方法样式不好控制,按钮的hover态以及active态不好处理。

2、使用label标签关联input

代码语言:javascript
复制
<label class="button" for="xFile">上传文件</label>
<input type="file" id="xFile" style="position:absolute;clip:rect(0 0 0 0);">
		<!-- clip 是对绝对定位进行裁剪,其余的地方默认隐藏rect(top,right,bottom,left),rect的参数都是距离左边或者上边的距离,如top与bottom是距离位裁剪前上面的距离,left与right距离左边的距离 -->

.file-input .button{display:block;width:100%;height:28px;line-height:28px;color:#fff;background:#0fd5d3;border:none;text-align:center;cursor:pointer;}

如果我们把选中的文件路径赋值给另一个div

代码语言:javascript
复制
<td id="fileAddress"></td>
<td><input type="file" id="fileinput"  style="width:75px;overflow:hidden;"/></td>
代码语言:javascript
复制
$(function () {
	    $("#fileinput").change(function(){
		 var file = $("#fileinput").val();
		 var filename=getFileName(file);
		 function getFileName(o){
		   var pos=o.lastIndexOf("\\");
		   return o.substring(pos+1); 
		 }
		 $("#fileAddress").html(filename);
		 
		})
	    
	});

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/07/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档