上傳檔案控制項的樣式美化

來源:互聯網
上載者:User

標籤:cursor   sof   city   logs   span   style   filter   load   pad   

實現原理:

第一步:分別建一個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>

運行結果:

美化前的預設樣式:

上傳檔案控制項的樣式美化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.