Skip to content

Instantly share code, notes, and snippets.

@javierartero
Created June 2, 2016 11:25
Show Gist options
  • Select an option

  • Save javierartero/3e8c3aeb28b77de9554a0b3f26f4034b to your computer and use it in GitHub Desktop.

Select an option

Save javierartero/3e8c3aeb28b77de9554a0b3f26f4034b to your computer and use it in GitHub Desktop.
File input single & multi
<!-- SINGLE -->
<div class="form-group">
<input type="file" id="file" class="inputfile">
<label for="file">
<span>Add 3D model</span>
</label>
</div>
<!-- MULTIPLE -->
<div class="form-group">
<input type="file" id="file" class="inputfile" multiple
data-multiple-caption="{count} files selected">
<label for="file">
<span>Add 3D model</span>
</label>
</div>
/*
By Osvaldas Valutis, www.osvaldas.info
Available for use under the MIT License
*/
'use strict';
;( function( $, window, document, undefined )
{
var $inputfile = $( '.inputfile' );
if ( $inputfile.length ) {
$inputfile.each( function(){
var $input = $( this ),
$label = $input.next( 'label' ),
labelVal = $label.html();
$input.on( 'change', function( e )
{
var fileName = '';
if( this.files && this.files.length > 1 )
fileName = ( this.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', this.files.length );
else if( e.target.value )
fileName = e.target.value.split( '\\' ).pop();
if( fileName )
$label.addClass('active').find( 'span' ).html( fileName );
else
$label.removeClass('active').html( labelVal );
});
// Firefox bug fix
$input
.on( 'focus', function(){ $input.addClass( 'has-focus' ); })
.on( 'blur', function(){ $input.removeClass( 'has-focus' ); });
});
}
})( jQuery, window, document );
.inputfile
width: 0.1px
height: 0.1px
opacity: 0
overflow: hidden
position: absolute
z-index: -1
&:focus + label,
& + label:hover
+focus-elements
& + label
display: inline-block
@extend .form-control
font-weight: inherit
&.active
span
color: $input-color
&:before
float: right
font-size: 20px
padding: 0
@extend .fa
@extend .fa-paperclip
*
pointer-events: none
span
display: block
margin-right: 40px
color: $input-color-placeholder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment