CSS 上传文件按钮:浏览器一致性

原理:添加一个a(btn:显示为button),添加一个file控件,将file与btn设置为相同大小,为了方便控制,最好将file添加到btn中,通过定位是file覆盖btn,同时设置file的visibility:hidden,这样在所有浏览器中看见的按钮为超链接a,点击按钮时实际是触发file。注:如果需要实现file选择文件后显示也是一致的话这需要JS的控制了(下面就是一款相应的插件,css需要自己去bootstrap网站下载映入);

 

/*
Bootstrap - File Input
======================
This is meant to convert all file input tags into a set of elements that displays consistently in all browsers.
Converts all
<input type="file">
into Bootstrap buttons
<a class="btn">Browse</a>
*/
$(function() {
$(‘input[type=file]‘).each(function(i,elem){
// Maybe some fields don‘t need to be standardized.
if (typeof $(this).attr(‘data-bfi-disabled‘) != ‘undefined‘) {
return;
}
// Set the word to be displayed on the button
var buttonWord = ‘Browse‘;
if (typeof $(this).attr(‘title‘) != ‘undefined‘) {
buttonWord = $(this).attr(‘title‘);
}
// Start by getting the HTML of the input element.
// Thanks for the tip http://stackoverflow.com/a/1299069
var $elem = $(elem);
var input = $(‘<div>‘).append( $elem.eq(0).clone() ).html();
// Now we‘re going to replace that input field with a Bootstrap button.
// The input will actually still be there, it will just be float above and transparent (done with the CSS).
$elem.replaceWith(‘<a class="file-input-wrapper btn ‘ + $elem.attr(‘class‘) + ‘">‘+buttonWord+input+‘</a>‘);
})
// After we have found all of the file inputs let‘s apply a listener for tracking the mouse movement.
// This is important because the in order to give the illusion that this is a button in FF we actually need to move the button from the file input under the cursor. Ugh.
.promise().done( function(){
// As the cursor moves over our new Bootstrap button we need to adjust the position of the invisible file input Browse button to be under the cursor.
// This gives us the pointer cursor that FF denies us
$(‘.file-input-wrapper‘).mousemove(function(cursor) {
var input, wrapper,
wrapperX, wrapperY,
inputWidth, inputHeight,
cursorX, cursorY;
// This wrapper element (the button surround this file input)
wrapper = $(this);
// The invisible file input element
input = wrapper.find("input");
// The left-most position of the wrapper
wrapperX = wrapper.offset().left;
// The top-most position of the wrapper
wrapperY = wrapper.offset().top;
// The with of the browsers input field
inputWidth= input.width();
// The height of the browsers input field
inputHeight= input.height();
//The position of the cursor in the wrapper
cursorX = cursor.pageX;
cursorY = cursor.pageY;
//The positions we are to move the invisible file input
// The 20 at the end is an arbitrary number of pixels that we can shift the input such that cursor is not pointing at the end of the Browse button but somewhere nearer the middle
moveInputX = cursorX - wrapperX - inputWidth + 20;
// Slides the invisible input Browse button to be positioned middle under the cursor
moveInputY = cursorY- wrapperY - (inputHeight/2);
// Apply the positioning styles to actually move the invisible file input
input.css({
left:moveInputX,
top:moveInputY
});
});
function resolveFileName (originalFileName) {
var fileName = originalFileName.replace(‘C:\\fakepath\\‘,‘‘);
if (fileName.lastIndexOf(‘\\‘) > 0) {
fileName = fileName.substr(fileName.lastIndexOf(‘\\‘)+1);
}
return fileName;
}
$(‘.file-input-wrapper input[type=file]‘).change(function(){
// Remove any previous file names
$(this).parent().next(‘.file-input-name‘).remove();
if ($(this).prop(‘files‘)) {
if ($(this).prop(‘files‘).length > 1) {
$(this).parent().after(‘<span class="file-input-name">‘+$(this)[0].files.length+‘ files</span>‘);
}
else {
$(this).parent().after(‘<span class="file-input-name">‘+resolveFileName($(this).val())+‘</span>‘);
}
}else if ($(this).val()) {
$(this).parent().after(‘<span class="file-input-name">‘+resolveFileName($(this).val())+‘</span>‘);
}
});
});
// Add the styles before the first stylesheet
// This ensures they can be easily overridden with developer styles
var cssHtml = ‘<style>‘+
‘.file-input-wrapper { overflow: hidden; position: relative; cursor: pointer; z-index: 1; }‘+
‘.file-input-wrapper input[type=file], .file-input-wrapper input[type=file]:focus, .file-input-wrapper input[type=file]:hover { position: absolute; top: 0; left: 0; cursor: pointer; opacity: 0; filter: alpha(opacity=0); z-index: 99; outline: 0; }‘+
‘.file-input-name { margin-left: 8px; }‘+
‘</style>‘;
$(‘link[rel=stylesheet]‘).eq(0).before(cssHtml);
}); 

注:使用上面的js代码只需要引入js就行,不同为file添加a。

<a class="file-input-wrapper btn">
Browse
<input type="file" value="" multiple="" id="file" name="fileContent">
</
a>

 

/*按钮显示*/

.btn {
  display: inline-block;
  *display: inline;
  /* IE7 inline-block hack */
  *zoom: 1;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  background-color: #f9f9f9;
  background-image: none;
  color: #418fde;
  text-shadow: none;
  border: 1px solid #dfdfdf;
  *border: 0;
  border-bottom-color: #c6c6c6;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  border-radius: 2px;
  *margin-left: .3em;
  -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
}

/*包裹file*/

.file-input-wrapper {
  cursor: pointer !important;
 position:relative;
}

 .file-input-wrapper input[type=file]{
    width:80px;
    opacity: 0;
    height: 30px;
    position: absolute;
    left:0;
    top:0;
    cursor: pointer !important;
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。