博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
上传文件控件的样式美化
阅读量:4604 次
发布时间:2019-06-09

本文共 2204 字,大约阅读时间需要 7 分钟。

实现原理:

第一步:分别建一个input[type=text]、一个input[type=button]标签,优化样式;

第二步:把input[type=file]标签,设置透明,位置对齐以上的两个标签并位于其底部;

第三步:用jquery实现把input[type=file]的文字显示在input[type=text]框内。

html:


<div class="uploader">

<input class="fileName" type="text" readonly="readonly">
<input class="upload_btn" type="button" value="Browse">
<input type="file">
<div class="clearfix"></div>
</div>


 

css:


<style type="text/css">

.clearfix{clear: both;}
input{margin:0;}
.uploader{
position: relative;
width:400px;
overflow:hidden;
}
.fileName{
border:1px solid #a6a6a6;
height:26px;
padding:2px 10px;
float:left;
width:80%;
box-sizing:border-box;
text-overflow:ellipsis;
border-right:0;
}
.upload_btn{
border:1px solid #71b5e0;
height:26px;
padding:2px 10px;
float:left;
width:20%;
box-sizing:border-box;
color:#333;
/*渐变背景色*/
background: -webkit-linear-gradient(#fff,#c9e4f5);
background: -moz-linear-gradient(#fff,#c9e4f5);
background: -o-linear-gradient(#fff,#c9e4f5);
background: linear-gradient(#fff,#c9e4f5);
background: -webkit-gradient(linear,left top,left bottom,color-stop(#fff),color-stop(#c9e4f5));/*chrome 4.0-9.0*/
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#FFF', endColorstr='#c9e4f5');/*ie9.0以下*/
}
.uploader:hover .upload_btn{
box-shadow:0 0 2px #bedef2 inset;
/*渐变背景色*/
background: -webkit-linear-gradient(#c9e4f5,#fff);
background: -moz-linear-gradient(#c9e4f5,#fff);
background: -o-linear-gradient(#c9e4f5,#fff);
background: linear-gradient(#c9e4f5,#fff);
background: -webkit-gradient(linear,left top,left bottom,color-stop(#c9e4f5),color-stop(#fff));/*chrome 4.0-9.0*/
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#c9e4f5', endColorstr='#fff');/*ie9.0以下*/
}
input[type=file]{
position:absolute;
left: 0;
top: 0;
bottom: 0;
right:0;
width:100%;
height:22px;
padding:4px 10px;
cursor: pointer;
filter:alpha(opacity:0);/*IE8以下*/
opacity:0;
}
</style>


 

Jquery:


<script>

$("input[type=file]").change(function(){
var x=$(this).val();
$(this).parent(".uploader").find(".fileName").val(x);
})
$("input[type=file]").each(function(){
$(this).parent(".uploader").find(".fileName").val("No file selected...");
})
</script>


运行结果:

美化前的默认样式:

转载于:https://www.cnblogs.com/janney/p/6646941.html

你可能感兴趣的文章
MySQL语法大全
查看>>
优先队列用法
查看>>
20140719 找到单链表的倒数第K个节点 判断一个链表是否成为一个环形 反转
查看>>
Confluence 6 基本性能问题诊断步骤
查看>>
MariaDB 服务器在 MySQL Workbench 备份数据的时候出错如何解决
查看>>
LinuxShell脚本——循环结构
查看>>
HDOJ 4008-Parent and son解题报告
查看>>
不写繁重的文档,就是敏捷吗
查看>>
抽屉协议
查看>>
Unity3d 基本设计开发 原则(提高代码可读性)
查看>>
【Codeforces 242C】King's Path
查看>>
【9305】蜜蜂路线
查看>>
.net根据模板对Word用Aspose Word操作
查看>>
批量导出数据库表(oracle)
查看>>
微信小程序编写物流信息进度样式
查看>>
星空:差分,状压dp
查看>>
Docker系列(八)Kubernetes介绍
查看>>
数据结构(三)实现AVL树
查看>>
iptables详解
查看>>
simpleCalculators
查看>>